SHARE
TWEET

Untitled

a guest Nov 13th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. import sqlite3
  3. import tkinter.messagebox as msg
  4. import tkinter as ttk
  5. import tkinter as tk
  6. from tkinter.ttk import Notebook
  7.  
  8. class Program(tk.Tk):
  9.  
  10.     def __init__(self, labels=None):
  11.         super().__init__()
  12.  
  13.         self.geometry("400x900")
  14.  
  15.         conn = sqlite3.connect("test.db")
  16.  
  17.         c = conn.cursor()
  18.  
  19.         if not labels:
  20.             self.labels = []
  21.         else:
  22.             self.labels = labels
  23.  
  24.         self.title("TKinter")
  25.  
  26.         self.notebook = Notebook(self)
  27.  
  28.         self.frame = ttk.Frame(self)
  29.  
  30.         self.canvas = tk.Canvas(self.frame)
  31.  
  32.         self.frame1 = ttk.Frame(self)
  33.  
  34.         self.canvas1 = tk.Canvas(self.frame1)
  35.  
  36.         # c.execute("""CREATE TABLE labels (first_label text)""")
  37.  
  38.         self.notebook.pack(side="top", fill="both", expand=1)
  39.  
  40.         self.canvas.pack(fill="both", expand=1)
  41.  
  42.         self.canvas1.pack(fill="both", expand=1)
  43.  
  44.         self.notebook.add(self.frame1, text="Tab 1")
  45.  
  46.         self.notebook.add(self.frame, text="Tab 2")
  47.  
  48.         self.another_entry = ttk.Entry(self.canvas1, width=30)
  49.         self.another_entry.place(x=0, y=0)
  50.         self.submit_button = ttk.Label(self.canvas1, text="Save Records")
  51.         self.submit_button.bind("<Button-1>", self.add_task)
  52.         self.submit_button.place(x=190, y=0)
  53.  
  54.         # c.execute("SELECT * FROM labels")
  55.         # records = c.fetchall()
  56.         # recorded = ""
  57.         # for record in records:
  58.         #     recorded += str(records[0]) + "\n"
  59.         # self.label = ttk.Label(self, text=recorded)
  60.         # self.label.place(x=900, y=0)
  61.  
  62.         conn.commit()
  63.         conn.close()
  64.  
  65.         task_current = self.load_task()
  66.         for record in task_current:
  67.             b = record[0]
  68.             self.add_task(None, b, True)
  69.     #To Save task
  70.  
  71.     def add_task(self, event=None, b=None, from_db=False):
  72.         if not b:
  73.             b = self.another_entry.get()
  74.  
  75.         if len(b) > 0:
  76.             msg.showinfo("About", "Task " + b + " " + "has been added")
  77.             self.new_task = ttk.Label(self.canvas, background="lightgrey", text=b)
  78.  
  79.             # new_task.bind("<Button-1>", self.remove_task)
  80.             self.new_task.pack(side="top", fill="x")
  81.             self.labels.append(self.new_task)
  82.             self.new_task.bind("<Button-1>", self.adr)
  83.             if not from_db:
  84.                 self.save_task(b)  
  85.         else:
  86.             msg.showerror("Error", "Blank Page! retry again")
  87.  
  88.         self.another_entry.delete(0, tk.END)
  89.    
  90.     def adr(self, event):
  91.         task = event.widget
  92.         msg.showinfo("About", "You clicked " + task.cget("text"))
  93.  
  94.     def save_task(self, task):
  95.         task_query = "INSERT INTO labels VALUES (?)"
  96.         task_data = (task,)
  97.         self.query(task_query, task_data)
  98.  
  99.     #To Show Records
  100.     @staticmethod
  101.     def query(sql, data=None, receive=False):
  102.         conn = sqlite3.connect("test.db")
  103.         cursor = conn.cursor()
  104.         if data:
  105.             cursor.execute(sql, data)
  106.         else:
  107.             cursor.execute(sql)
  108.        
  109.         if receive:
  110.             return cursor.fetchall()
  111.         else:
  112.             conn.commit()
  113.         conn.close()
  114.  
  115.     # def database2(self, event=None):
  116.     #     conn = sqlite3.connect("test.db")
  117.     #     c = conn.cursor()
  118.     #     task_query = "INSERT INTO labels VALUES (?)"
  119.     #     default = "----------ADD ITEMS HERE----------"
  120.     #     main.query(task_query, default)
  121.     #     # c.execute("SELECT * FROM labels")
  122.     #     # records = c.fetchall()
  123.     #     # self.recorded = ""
  124.     #     # # for record in records:
  125.     #     # #     self.recorded += str(record[0]) + "\n"
  126.  
  127.         # conn.commit()
  128.         # conn.close()
  129.         # msg.showinfo("About", "Records has been added!")
  130.         # self.another_entry.delete(0, tk.END)
  131.  
  132.     def load_task(self):
  133.         load_task = "SELECT first_label FROM labels"
  134.         my_tasks = self.query(load_task, receive=True)
  135.         return my_tasks
  136.  
  137.     def info(self, event=None):
  138.         msg.showinfo("About", "This button is" + " " + self.recorded)
  139.        
  140.     def exit(self, event=None):
  141.         if msg.askyesno("About", "Are you sure you want to exit?"):
  142.             main.destroy()
  143.         else:
  144.             return
  145.  
  146. if __name__ == "__main__":
  147.     main = Program()
  148.     main.protocol("WM_DELETE_WINDOW", main.exit)
  149.     main.iconbitmap("icon/5848152fcef1014c0b5e4967.ico")
  150.     main.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top