Python Forum
Auto increament in Entry field. in tkinter GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto increament in Entry field. in tkinter GUI
#7
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


Reply


Messages In This Thread
RE: Auto increament in Entry field. in tkinter GUI - by menator01 - Sep-16-2021, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter only storing last element/data from entry widget bertschj1 8 1,002 May-06-2025, 11:54 PM
Last Post: deanhystad
  Entry field random pull from list, each return own line Bear1981 6 1,049 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  how to open a popup window in tkinter with entry,label and button lunacy90 1 3,678 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Tkinter Entry size limit vman44 3 11,896 Dec-22-2022, 06:58 AM
Last Post: vman44
  Display value in the another entry field instead of message box adisc 6 2,946 Jun-25-2022, 02:30 PM
Last Post: rob101
  tkinter auto press button kucingkembar 2 5,203 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  Bug ? when dataclass field name == field type Cyril 0 2,103 Oct-22-2020, 03:26 AM
Last Post: Cyril
  Help pulling tkinter Entry string data Raudert 8 12,095 Jan-12-2017, 11:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020