Python Forum
[Tkinter] Not getting entry values on button click & treeview not updating ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Not getting entry values on button click & treeview not updating ?
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.
  1. First problem.
    My code creates a gui with input boxes, a button, and a treeview.
    When I click the button, it runs my adding_records method, but it adds blank records.
    From what I can gather, when the button is clicked, it runs the adding_records() method before the entries are able to be set into variables, thus the variables being passed to the method are empty.
    From what I searched, the lambda: in the command is supposed to help that, but it is not helping.

  2. Problem 2.
    When a record is added via the button and method, it does not refresh in the treeview.
    I must close out the program and re-run it for it to show up.
    I have tried placing it in the mainloop per some searching, but no success.

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)
                return query_result

        #----------------------Method to populate Treeview-----------    
        def viewing_records():
                records = tree.get_children()
                for element in records:
                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)

        frameb = tk.Frame(width=800, height=500)

        #---------------------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-------------------



if __name__=='__main__':
    win = tk.Tk()
    application = PriceList(win)
In answer to your questions:
  1. The lambda is set to the the values of the entry's at the time the lambda is created.
    It would be better to add a function that is called when the button is clicked,
    it reads the values of the entry's and then calls adding_records with the current values.

  2. The records are added to the database but the treeview has not not been given a way to show the updates.
    Either add the new items or clearing the treeview and calling viewing_records()
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.
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.
Got it working, thank you for your help!

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Modify Class on Button Click KDog 4 1,243 May-11-2021, 08:43 PM
Last Post: KDog
  Updating button text based upon different variable values knoxvilles_joker 0 240 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  [Tkinter] Justify the treeview to the last entry MrP 7 1,192 Mar-01-2021, 11:20 AM
Last Post: MrP
  [Tkinter] Can I modify part of 'Values' value of Treeview item? water 0 361 Dec-29-2020, 09:35 PM
Last Post: water
  [Tkinter] Button click problem using OOP JohnB 5 664 Oct-21-2020, 12:43 PM
Last Post: JohnB
  tkinter | Button color text on Click Maryan 2 720 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Tkinter adding entry values scratchmyhead 1 707 May-04-2020, 05:21 AM
Last Post: Yoriz
  Closing window on button click not working kenwatts275 4 761 May-03-2020, 01:59 PM
Last Post: deanhystad
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 1,349 Apr-22-2020, 05:47 AM
Last Post: iconit
  [Tkinter] Treeview expand button size shamnadinn 8 2,361 Mar-08-2020, 12:02 PM
Last Post: shamnadinn

Forum Jump:

User Panel Messages

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