[Tkinter] Not getting entry values on button click & treeview not updating ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Not getting entry values on button click & treeview not updating ? (/thread-18187.html) |
Not getting entry values on button click & treeview not updating ? - swanysto - May-08-2019 So I am very new to programming, I have set a goal of creating a simple file for tracking material prices that I use often. I have been rummaging through tutorials and youtube videos to learn stuff. I have a long way to go, but I am caught up on a couple items that if I get figured out will set me on the right path.
from tkinter import ttk import tkinter as tk import sqlite3 class PriceList: def __init__(self, win): #----------------------Runs SQL Queries-------------------------------- def run_query(query, parameters = ()): with sqlite3.connect('pricing.db') as conn: cursor = conn.cursor() query_result = cursor.execute(query, parameters) conn.commit() return query_result #----------------------Method to populate Treeview----------- def viewing_records(): records = tree.get_children() for element in records: self.tree.delete(element) query = 'SELECT * FROM parts' db_rows = run_query(query) for row in db_rows: tree.insert('', 0, text = row[0], values = (row[0], row[1], row[2], row[3], row[4], row[5])) def adding_records(matID, descr, manuf, price, unit, condition): query = "INSERT INTO parts VALUES (?, ?, ?, ?, ?, ?)" run_query(query, (matID, descr, manuf, price, unit, condition)) #---------------------Sets up Gui Window and Sets Size------------------ win.minsize(width=800, height=600) win.resizable(width=0, height=0) win.title('Morfin Price List') framet = tk.Frame(width=800, height=300) framet.pack(side="top") frameb = tk.Frame(width=800, height=500) frameb.pack(side="bottom") #---------------------Sets up labels and entry boxes------------------ label1 = tk.Label(framet, text='Material ID: ', width=15) label1.grid(row=1, column=1) e1 = tk.Entry(framet, width=20) e1.grid(row=1, column=2) label2 = tk.Label(framet, text='Description: ', width=15) label2.grid(row=2, column=1) e2 = tk.Entry(framet, width=30) e2.grid(row=2, column=2) label3 = tk.Label(framet, text='Manufacturer: ', width=15) label3.grid(row=3, column=1) e3 = tk.Entry(framet, width=20) e3.grid(row=3, column=2) label4 = tk.Label(framet, text='Price: ', width=15) label4.grid(row=4, column=1) e4 = tk.Entry(framet, width=20) e4.grid(row=4, column=2) label5 = tk.Label(framet, text='Unit: ', width=15) label5.grid(row=5, column=1) e5 = tk.Entry(framet, width=20) e5.grid(row=5, column=2) label6 = tk.Label(framet, text='Condition: ', width=15) label6.grid(row=6, column=1) e6 = tk.Entry(framet, width=20) e6.grid(row=6, column=2) #--------------------------sets entry data into variables to pass to method----- var1 = e1.get() var2 = e2.get() var3 = e3.get() var4 = e4.get() var5 = e5.get() var6 = e6.get() #----creates button with command to run adding_records() method on click--------- b1 = tk.Button(framet, text='Add', command=lambda : adding_records(var1,var2,var3, var4, var5, var6)) b1.grid(row=1, column=7) #----------------------Build Treeview----------------------------------- tree=ttk.Treeview(frameb, selectmode='browse') tree.grid(row=1, column=1) tree["columns"] = ("1", "2", "3", "4", "5", "6") tree["show"] = 'headings' tree.column("1", width=120, anchor='c') tree.column("2", width=190, anchor='c') tree.column("3", width=150, anchor='c') tree.column("4", width=100, anchor='c') tree.column("5", width=80, anchor='c') tree.column("6", width=150, anchor='c') tree.heading("1", text="Material ID") tree.heading("2", text="Description") tree.heading("3", text="Manufacturer") tree.heading("4", text="Price") tree.heading("5", text="Unit") tree.heading("6", text="Condition") #-----------------Calls method to populate Treeview------------------- viewing_records() if __name__=='__main__': win = tk.Tk() application = PriceList(win) win.mainloop() RE: Tkinter/sqlite method issues - Yoriz - May-08-2019 In answer to your questions:
RE: Tkinter/sqlite method issues - swanysto - May-08-2019 Thank you for your reply 1. I am not quite sure what that entails. So let's say I replace the call for adding_records in the current button. I replace it with something like get_values. Would I pass the entry objects to the function? (i.e. get_values(e1, e2, etc). Then inside the function call the get() function? Wouldn't I run into the same issue of the Entry object not being set before it is passed to the function? def get_value(e1,e2,e3,e4,e5,e6): e1 = e1.get() e2 = e2.get() ...... adding_records(e1, e2.......)2. Perfect, I think I got that worked out. I added a line in the adding_records that inserts the items into the treeview. So far that seems to be working. RE: Tkinter/sqlite method issues - Yoriz - May-09-2019 The difference would be, get is called every time the button click happens instead of only once before clicking the button. Also see this post for an example of using classes with tkinter. RE: Not getting entry values on button click & treeview not updating ? - swanysto - May-10-2019 Got it working, thank you for your help! |