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.
- 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.
- 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()
