Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Not getting entry values on button click & treeview not updating ?
#1
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)
                    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()
	
Quote
#2
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()
Quote
#3
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.
Quote
#4
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.
Quote
#5
Got it working, thank you for your help!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 235 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [PyQt] How to populate a treeview on a GUI with a dictionary mart79 1 616 Aug-05-2019, 01:30 PM
Last Post: Denni
  [PyQt] Problem how to click a button inside a group box? mart79 2 246 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 409 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 671 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 633 May-12-2019, 08:17 PM
Last Post: joe_momma
  [Tkinter] Image does not show in treeview. KevinBrown 3 593 May-05-2019, 11:47 PM
Last Post: KevinBrown
  Simple Button click on image file to create action? jpezz 4 517 Mar-27-2019, 10:08 PM
Last Post: jpezz
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 628 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Tkinter timetabl using treeview mntfr 3 449 Feb-05-2019, 09:36 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)