Sep-16-2021, 12:19 AM
(This post was last modified: Sep-18-2021, 03:28 PM by menator01.
Edit Reason: Added a display for the database entries
)
I done a version of your code
#! /usr/bin/env python3 # Do the imports import sqlite3 import tkinter as tk from functools import partial # Create/Connect to the sqlite3 database conn = sqlite3.connect('test.db') cursor = conn.cursor() cursor.execute('drop table if exists test_table') conn.commit() # Create the table conn = sqlite3.connect('test.db') cursor = conn.cursor() table = '''create table test_table( id integer primary key autoincrement not null, name varchar(100) not null )''' conn.execute(table) conn.commit() # A list of entries for the database names = [(1,'tom'), (2, 'harry'), (3, 'john'),(4, 'mary'), (5, 'pam')] # Insert the data into the test database conn = sqlite3.connect('test.db') cursor = conn.cursor() cursor.executemany('insert into test_table (id, name) values (?, ?)', names) conn.commit() conn.close() # A couple of global variables database = 'test.db' table = 'test_table' # Create the class for the widget class Window: def __init__(self, parent): self.parent = parent self.parent.columnconfigure(0, weight=1) self.parent.rowconfigure(0, weight=1) # Create the id columns and rows for the entry fields id_label = tk.Label(self.parent, text='Next ID:', anchor='w') id_label['bg'] = 'light blue' id_label['relief'] = 'ridge' id_label.grid(column=0, row=0, sticky='new') self.count = tk.IntVar() self.count.set(self.get_count()) id_number = tk.Entry(self.parent, text=self.count) id_number.grid(column=1, row=0, sticky='new') work_label = tk.Label(self.parent, text='Work Required:', anchor='w') work_label['bg'] = 'light blue' work_label['relief'] = 'ridge' work_label.grid(column=0,row=1, sticky='new', pady=5) string = tk.StringVar() self.entry = tk.Entry(self.parent, textvariable=string) self.entry.focus() self.entry.grid(column=1, row=1, sticky='new', pady=5) # Create the submit button btn = tk.Button(self.parent, text='Submit', command=partial(self.addwork, id_number, self.entry)) btn.grid(column=1, row=2, sticky='new') self.parent.bind('<Return>', partial(self.addwork, id_number, self.entry)) self.parent.bind('<KP_Enter>', partial(self.addwork, id_number, self.entry)) # Create a container frame for the data self.dataframe = tk.Frame(self.parent) self.dataframe['relief'] = 'flat' self.dataframe['borderwidth'] = 1 self.dataframe['highlightbackground'] = 'black' self.dataframe['highlightcolor'] = 'black' self.dataframe['highlightthickness'] = 1 self.dataframe.grid(column=0, row=3, columnspan=2, sticky='new', pady=8) self.dataframe.grid_columnconfigure(0, weight=1) self.dataframe.grid_columnconfigure(1, weight=3) # Create a header row label = tk.Label(self.dataframe, text='ID', anchor='w', relief='raised', bg='lightblue') label.grid(column=0, row=0, sticky='new') label = tk.Label(self.dataframe, text='Name', anchor='w', relief='raised', bg='lightblue') label.grid(column=1, row=0, sticky='new') # Get the data and display row = 1 for data in self.get_info(): label = tk.Label(self.dataframe, text=data[0], anchor='w', bg='lightblue') label['relief'] = 'ridge' label['padx'] = 8 label.grid(column=0, row=row, sticky='new') row += 1 row = 1 for data in self.get_info(): label = tk.Label(self.dataframe, text=data[1].capitalize(), anchor='w', bg='lightblue') label['relief'] = 'ridge' label['padx'] = 8 label.grid(column=1, row=row, sticky='new') row += 1 # Create the method for inserting the submitted data into the database def addwork(self, id_number, entry, event=None): info = [(id_number.get(), entry.get())] conn = sqlite3.connect(database) conn.executemany(f'insert into {table}(id, name) values (?,?)', info) conn.commit() conn.close() # Update the count display and clear the entry field update = self.get_count() self.count.set(update) self.entry.delete(0, tk.END) # Update the displayed data entries row = 1 for data in self.get_info(): label = tk.Label(self.dataframe, text=data[0], anchor='w', bg='lightblue') label['relief'] = 'ridge' label['padx'] = 8 label.grid(column=0, row=row, sticky='new') row += 1 row = 1 for data in self.get_info(): label = tk.Label(self.dataframe, text=data[1].capitalize(), anchor='w', bg='lightblue') label['relief'] = 'ridge' label['padx'] = 8 label.grid(column=1, row=row, sticky='new') row += 1 # Method for getting entry count def get_count(self): conn = sqlite3.connect(database) cursor = conn.cursor() count = conn.execute(f'select count(*) from {table}') next_num = count.fetchall()[0][0]+1 conn.close() return next_num # Method for getting the data from the database def get_info(self): conn = sqlite3.connect(database) cursor = conn.cursor() data = conn.execute(f'select * from {table}') info = data.fetchall() conn.close() return info # Create the root window and init the Window class def main(): root = tk.Tk() root.title('ADD Work') root.geometry('+250+250') root.resizable(0, 0) root['bg'] = 'light blue' root['padx'] = 10 root['pady'] = 5 Window(root) root.mainloop() if __name__ == '__main__': main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts