Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import sqlite3
- import tkinter.messagebox as msg
- import tkinter as ttk
- import tkinter as tk
- from tkinter.ttk import Notebook
- class Program(tk.Tk):
- def __init__(self, labels=None):
- super().__init__()
- self.geometry("400x900")
- conn = sqlite3.connect("test.db")
- c = conn.cursor()
- if not labels:
- self.labels = []
- else:
- self.labels = labels
- self.title("TKinter")
- self.notebook = Notebook(self)
- self.frame = ttk.Frame(self)
- self.canvas = tk.Canvas(self.frame)
- self.frame1 = ttk.Frame(self)
- self.canvas1 = tk.Canvas(self.frame1)
- # c.execute("""CREATE TABLE labels (first_label text)""")
- self.notebook.pack(side="top", fill="both", expand=1)
- self.canvas.pack(fill="both", expand=1)
- self.canvas1.pack(fill="both", expand=1)
- self.notebook.add(self.frame1, text="Tab 1")
- self.notebook.add(self.frame, text="Tab 2")
- self.another_entry = ttk.Entry(self.canvas1, width=30)
- self.another_entry.place(x=0, y=0)
- self.submit_button = ttk.Label(self.canvas1, text="Save Records")
- self.submit_button.bind("<Button-1>", self.add_task)
- self.submit_button.place(x=190, y=0)
- # c.execute("SELECT * FROM labels")
- # records = c.fetchall()
- # recorded = ""
- # for record in records:
- # recorded += str(records[0]) + "\n"
- # self.label = ttk.Label(self, text=recorded)
- # self.label.place(x=900, y=0)
- conn.commit()
- conn.close()
- task_current = self.load_task()
- for record in task_current:
- b = record[0]
- self.add_task(None, b, True)
- #To Save task
- def add_task(self, event=None, b=None, from_db=False):
- if not b:
- b = self.another_entry.get()
- if len(b) > 0:
- msg.showinfo("About", "Task " + b + " " + "has been added")
- self.new_task = ttk.Label(self.canvas, background="lightgrey", text=b)
- # new_task.bind("<Button-1>", self.remove_task)
- self.new_task.pack(side="top", fill="x")
- self.labels.append(self.new_task)
- self.new_task.bind("<Button-1>", self.adr)
- if not from_db:
- self.save_task(b)
- else:
- msg.showerror("Error", "Blank Page! retry again")
- self.another_entry.delete(0, tk.END)
- def adr(self, event):
- task = event.widget
- msg.showinfo("About", "You clicked " + task.cget("text"))
- def save_task(self, task):
- task_query = "INSERT INTO labels VALUES (?)"
- task_data = (task,)
- self.query(task_query, task_data)
- #To Show Records
- @staticmethod
- def query(sql, data=None, receive=False):
- conn = sqlite3.connect("test.db")
- cursor = conn.cursor()
- if data:
- cursor.execute(sql, data)
- else:
- cursor.execute(sql)
- if receive:
- return cursor.fetchall()
- else:
- conn.commit()
- conn.close()
- def load_task(self):
- load_task = "SELECT first_label FROM labels"
- my_tasks = self.query(load_task, receive=True)
- return my_tasks
- def info(self, event=None):
- msg.showinfo("About", "This button is" + " " + self.recorded)
- def exit(self, event=None):
- if msg.askyesno("About", "Are you sure you want to exit?"):
- main.destroy()
- else:
- return
- if __name__ == "__main__":
- main = Program()
- main.protocol("WM_DELETE_WINDOW", main.exit)
- main.iconbitmap("icon/5848152fcef1014c0b5e4967.ico")
- main.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement