Submit tkinter entry data to sqlite3 db - python

Complete noob here, I'm trying to submit data from a group of tkinter entry boxes to sqlite3. When I run the code, the form displays correctly and the table is created, but the data is not being submitted to the database. I get an error saying that "submit_record", the function connected to the submit button, is not defined. I've tried everything I know or could find online. Please help if you can. Been struggling with this one thing for 2 weeks! Thanks.
from tkinter import *
from tkinter import ttk
import sqlite3
LARGE_FONT = ('Veranda', 12)
MED_FONT = ('Veranda', 10, 'bold')
SMALL_FONT = ('Helvetica', 9, 'bold')
class CACFP_DB:
def setup_db(self):
# Open db
self.conn = sqlite3.connect('cacfp_test.db')
# Create a cursor
self.c = self.conn.cursor()
# Create the table if it doesn't exist
try:
self.c.execute("CREATE TABLE if not exists Staffing(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, lname TEXT NOT NULL, fname TEXT NOT NULL, title TEXT NOT NULL, department TEXT NOT NULL, payType TEXT NOT NULL, start INTEGER NOT NULL, end_dt INTEGER NOT NULL, t_hours INTEGER NOT NULL, perinc REAL NOT NULL, inc_type TEXT NOT NULL, inc_date INTEGER NOT NULL, hrs_month INTEGER NOT NULL, month_yr INTEGER NOT NULL, c_hours REAL NOT NULL, per_c_hours REAL NOT NULL, gross_hrly REAL NOT NULL, gross_hrly_entry REAL NOT NULL, emp_tax REAL NOT NULL, ue_tax REAL NOT NULL, total_tax REAL NOT NULL, cost_per_emp REAL NOT NULL, cost_total TEXT NOT NULL);")
self.conn.commit()
except sqlite3.OperationalError:
print("ERROR: Table not Created")
def submit_record(self):
# Insert record into the db
staff_dtls = [self.lname_entry.get(), self.fname_entry.get(), self.title_entry.get(),
self.department_entry.get(), self.payType_entry.get(), self.start_entry.get(),
self.end_dt_entry.get(), self.t_hours_entry.get(), self.perinc_entry.get(),
self.inc_type.get(), self.inc_date_entry.get(), self.hrs_month_entry.get(),
self.month_yr_entry.get(), self.c_hours_entry.get(), self.per_c_hours_entry.get(),
self.gross_hrly_entry.get(), self.gross_monthly_entry.get(), self.emp_tax_entry.get(),
self.ue_tax_entry.get(), self.total_tax_entry.get()]
for element in staff_dtls:
c.excecute("INSERT INTO staffing VALUES (?)", (element))
self.conn.commit()
self.c.close()
self.conn.close()
def __init__(self, root):
root.title('CACFP (CACFP_developmant_version.py)')
root.geometry('1200x600')
root.state('zoomed')
mainlabel = ttk.Label(root, text='\nBudget Detail Worksheet', font=LARGE_FONT, justify=CENTER)
mainlabel.grid(row=0, column=0, columnspan=50, padx=5, sticky=W + E)
subtext = ttk.Label(root, text='Sponsors and Affiliated Sites')
subtext.grid(row=1, column=0, padx=5, sticky=W + E)
filler = ttk.Label(root, text='------')
filler.grid(row=3, column=0, padx=25)
# ----- Create Notebook -----#
nb = ttk.Notebook(root)
nb.grid(row=3, column=0, columnspan=50, rowspan=49, padx=5, pady=5, sticky='NESW')
# ----- Notebook Tab1 -----#
tab1 = ttk.Frame(nb)
nb.add(tab1, text='Staffing')
# tab1.grid_columnconfigure(8, minsize=10)
# ----- Tab1 Headings (row 0) ----- #
emp_head = ttk.Label(tab1, text='\nEMPLOYEE DETAILS', font=MED_FONT)
emp_head.grid(row=0, column=0, columnspan=12, sticky=W + E)
# ---- Frames (boxes) within tab1 ------
box1 = ttk.Frame(tab1, width=600, height=75, relief='raised', borderwidth='1')
box1.grid(row=1, column=0, padx=10, pady=20, sticky=W)
box2 = ttk.Frame(tab1, width=600, height=75, relief='raised', borderwidth='1')
box2.grid(row=3, column=0, padx=10, pady=20, sticky=W)
# ---- Box 1 labels and entry boxes -----
lname = ttk.Label(box1, text='\nLast Name', font=SMALL_FONT, justify=CENTER)
lname.grid(row=1, column=0, padx=2, pady=5, sticky='W')
lname_entry_value = StringVar(box1, value="")
lname_entry = ttk.Entry(box1, textvariable=lname_entry_value)
lname_entry.grid(row=2, column=0, padx=2, pady=5, sticky=W)
fname = ttk.Label(box1, text='\nFirst Name', font=SMALL_FONT, justify=CENTER)
fname.grid(row=1, column=1, padx=2, pady=5, sticky='W')
fname_entry_value = StringVar(box1, value="")
fname_entry = ttk.Entry(box1, textvariable=fname_entry_value)
fname_entry.grid(row=2, column=1, padx=2, pady=5, sticky=W)
title = ttk.Label(box1, text='\nPosition Title', font=SMALL_FONT, justify=CENTER)
title.grid(row=1, column=2, padx=2, pady=5, sticky='W')
title_entry_value = StringVar(box1, value="")
title_entry = ttk.Entry(box1, textvariable=title_entry_value)
title_entry.grid(row=2, column=2, padx=2, pady=5, sticky=W)
department = ttk.Label(box1, width=15, text='\nDeptartment', font=SMALL_FONT, justify=CENTER)
department.grid(row=1, column=3, padx=2, pady=5, sticky='W')
department_entry_value = StringVar(box1, value="")
department_entry = ttk.Entry(box1, textvariable=department_entry_value)
department_entry.grid(row=2, column=3, padx=2, pady=5, sticky='W')
payType = ttk.Label(box1, width=15, text='Pay Type\n(Hourly or Monthly)', font=SMALL_FONT, justify=CENTER)
payType.grid(row=1, column=4, padx=2, pady=5, sticky='W')
payType_entry_value = StringVar(box1, value="")
payType_entry = ttk.Entry(box1, textvariable=payType_entry_value)
payType_entry.grid(row=2, column=4)
start = ttk.Label(box1, text='\nStart Time', font=SMALL_FONT, justify=CENTER)
start.grid(row=1, column=5, padx=5, pady=5, sticky='W')
start_entry_value = StringVar(box1, value="")
start_entry = ttk.Entry(box1, width=12, textvariable=start_entry_value)
start_entry.grid(row=2, column=5, padx=2, pady=5, sticky=W)
end_dt = ttk.Label(box1, text='\nEnd Time', font=SMALL_FONT, justify=CENTER)
end_dt.grid(row=1, column=6, padx=5, pady=5, sticky='W')
end_dt_entry_value = StringVar(box1, value="")
end_dt_entry = ttk.Entry(box1, width=12, textvariable=end_dt_entry_value)
end_dt_entry.grid(row=2, column=6, padx=2, pady=5, sticky=W)
t_hours = ttk.Label(box1, text='Total Work\nHours', font=SMALL_FONT, justify=CENTER)
t_hours.grid(row=1, column=7, padx=5, pady=5, sticky='W')
t_hours_entry_value = IntVar(box1, value="")
t_hours_entry = ttk.Entry(box1, width=12, textvariable=t_hours_entry_value)
t_hours_entry.grid(row=2, column=7, padx=2, pady=5, sticky=W)
perinc = ttk.Label(box1, width=10, text='Annual %\nIncrease', font=SMALL_FONT, justify=CENTER)
perinc.grid(row=1, column=8, padx=5, pady=5, sticky='W')
perinc_entry_value = StringVar(box1, value="")
perinc_entry = ttk.Entry(box1, width=12, textvariable=perinc_entry_value)
perinc_entry.grid(row=2, column=8, padx=2, pady=5, sticky=W)
inc_type = ttk.Label(box1, text='Increase Type\n(Merit or COL)', font=SMALL_FONT, justify=CENTER)
inc_type.grid(row=1, column=9, padx=2, pady=5, sticky='W')
inc_type_entry_value = StringVar(box1, value="")
inc_type_entry = ttk.Entry(box1, width=15, textvariable=inc_type_entry_value)
inc_type_entry.grid(row=2, column=9)
inc_date = ttk.Label(box1, text='Increase\nDate', font=SMALL_FONT, justify=CENTER)
inc_date.grid(row=1, column=10, padx=5, pady=5, sticky='W')
inc_date_entry_value = StringVar(box1, value="")
inc_date_entry = ttk.Entry(box1, width=14, textvariable=inc_date_entry_value)
inc_date_entry.grid(row=2, column=10, padx=2, pady=5, sticky=W)
# ---- Box 2 labels and entry boxes for tab1 -----
hrs_month = ttk.Label(box2, text='Total Work\nHrs/Month', font=SMALL_FONT, justify=CENTER)
hrs_month.grid(row=1, column=0, padx=5, pady=5, sticky='W')
hrs_month_entry_value = StringVar(box2, value="")
hrs_month_entry = ttk.Entry(box2, width=12, textvariable=hrs_month_entry_value)
hrs_month_entry.grid(row=2, column=0, padx=2, pady=5, sticky=W)
month_yr = ttk.Label(box2, text='Total Work\nMonths/Yr', font=SMALL_FONT, justify=CENTER)
month_yr.grid(row=1, column=1, padx=5, pady=5, sticky='W')
month_yr_entry_value = StringVar(box2, value="")
month_yr_entry = ttk.Entry(box2, width=12, textvariable=month_yr_entry_value)
month_yr_entry.grid(row=2, column=1, padx=2, pady=5, sticky=W)
c_hours = ttk.Label(box2, text='Total Hrs Worked\nfor CACFP', font=SMALL_FONT, justify=CENTER)
c_hours.grid(row=1, column=2, padx=2, pady=5, sticky='W')
c_hours_entry_value = StringVar(box2, value="")
c_hours_entry = ttk.Entry(box2, textvariable=c_hours_entry_value)
c_hours_entry.grid(row=2, column=2, padx=2, pady=5, sticky=W)
per_c_hours = ttk.Label(box2, text='% Hrs Worked\nfor CACFP', font=SMALL_FONT, justify=CENTER)
per_c_hours.grid(row=1, column=3, padx=2, pady=5, sticky='W')
per_c_hours_entry_value = StringVar(box2, value="")
per_c_hours_entry = ttk.Entry(box2, textvariable=per_c_hours_entry_value)
per_c_hours_entry.grid(row=2, column=3, padx=2, pady=5, sticky=W)
gross_hrly = ttk.Label(box2, text='Gross Hourly\n(before deductions)', font=SMALL_FONT, justify=CENTER)
gross_hrly.grid(row=1, column=4, padx=5, pady=5, sticky='W')
gross_hrly_entry_value = StringVar(box2, value="")
gross_hrly_entry = ttk.Entry(box2, textvariable=gross_hrly_entry_value)
gross_hrly_entry.grid(row=2, column=4, padx=2, pady=5, sticky=W)
gross_monthly = ttk.Label(box2, text='Gross Monthly\n(before deductions)', font=SMALL_FONT, justify=CENTER)
gross_monthly.grid(row=1, column=5, padx=5, pady=5, sticky='W')
gross_monthly_entry_value = StringVar(box2, value="")
gross_monthly = ttk.Entry(box2, textvariable=gross_monthly_entry_value)
gross_monthly.grid(row=2, column=5, padx=2, pady=5, sticky=W)
emp_tax = ttk.Label(box2, text='Employer Tax\nPer Month', font=SMALL_FONT, justify=CENTER)
emp_tax.grid(row=1, column=6, padx=5, pady=5, sticky='W')
emp_tax_entry_value = StringVar(box2, value="")
emp_tax = ttk.Entry(box2, textvariable=emp_tax_entry_value)
emp_tax.grid(row=2, column=6, padx=2, pady=5, sticky=W)
ue_tax = ttk.Label(box2, text='Unemployment\nTax Rate/Month', font=SMALL_FONT, justify=CENTER)
ue_tax.grid(row=1, column=7, padx=5, pady=5, sticky='W')
ue_tax_entry_value = StringVar(box2, value="")
ue_tax = ttk.Entry(box2, textvariable=ue_tax_entry_value)
ue_tax.grid(row=2, column=7, padx=2, pady=5, sticky=W)
total_tax = ttk.Label(box2, text='Total Tax Paid\nPer Month', font=SMALL_FONT, justify=CENTER)
total_tax.grid(row=1, column=8, padx=5, pady=5, sticky='W')
total_tax_entry_value = StringVar(box2, value="")
total_tax = ttk.Entry(box2, textvariable=total_tax_entry_value)
total_tax.grid(row=2, column=8, padx=2, pady=5, sticky=W)
cost_per_emp = ttk.Label(box2, width=13, text='Annual Cost\nto CACFP', font=SMALL_FONT, justify=CENTER)
cost_per_emp.grid(row=1, column=9)
cost_per_emp = ttk.Label(box2, width=12, text='$ 00,000.00', relief=SUNKEN, borderwidth=0)
cost_per_emp.grid(row=2, column=9, padx=2, pady=5, sticky=W)
cost_per_emp.configure(background='white')
# ----- Root Command Bar (works with tab1 data)
cmd_bar = ttk.Frame(root, width=150, height=615, relief='raised', borderwidth='2')
cmd_bar.grid(row=6, column=100, sticky=NSEW)
cmd_bar_title = ttk.Label(cmd_bar, text='Command Bar', font=MED_FONT, justify=CENTER)
cmd_bar_title.grid(row=0, column=0, pady=5, padx=10, sticky=N)
cmd_bar.grid_propagate(0)
# ----- Action Buttons -----
submit_1 = ttk.Button(cmd_bar, text='Submit', command=lambda: self.submit_record())
submit_1.grid(row=3, column=0, pady=15)
# # ----- End Tab1 Detail Labels ----- #
self.setup_db()
root = Tk()
db = CACFP_DB(root)
root.mainloop()

You have in your code more inconsistencies.
Firstly, when you have in staff_dtls e.g. self.lname_entry.get(), you have to have defined self.lname_entry, it's not sufficient to have only lname_entry, because of Python Classes and OOP theory.
Next, you have some mess in variable names e.g. in staff_dtls you have self.inc_type.get(), but you have to have self.inc_type_entry.get() and vice versa, you have e.g.
ue_tax = ttk.Entry(box2, textvariable=ue_tax_entry_value), but you need ue_tax_entry = ttk.Entry(box2, textvariable=ue_tax_entry_value). So unite precisely your variable names.
Next, instead of c.excecute("INSERT INTO staffing VALUES (?)", (element)) (with typo excecute), you need something like this self.c.execute("INSERT INTO staffing VALUES (?, ?, ?, ?, ?, ?, ...)", (element)), with so many question marks, how many columns you want to insert to DB.
These are the main errors when you fix them, you will be on the right track.

Related

How to create multiple Treeview window with same format/layout upon clicking a button in Python Tkinter?

Currently I have one window that displays two Treeview frames(festo and transformer) that of identical layout. These treeview layout should display different user data from txt files. Hence, the no of Treeview windows are dynamic based on the no txt files in a folder. Is it possible to create/generate multiple pop-up Treeview windows of the same layout upon a button click?
class HomePage(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.notebook = ttk.Notebook() # Create a notebook widget
self.add_tab1()
#self.add_tab2()
self.notebook.grid(row=0)
self.notebook.pack(expand=1, fill="both")
def add_tab1(self):
tab1 = tab_one(self.notebook)
self.notebook.add(tab1, text="Home")
def add_tab2(self):
tab2 = ttk.Notebook()
self.notebook.add(tab2, text="Reports")
class data_table(object):
def __init__(self, site, panels_count, tev_count):
self.site = site
self.panels_count = panels_count
self.tev_count = tev_count
class tab_one(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
# Creating frames on the window/canvas for the first tab
frame_selectfile = tk.LabelFrame(self, text="Step 1: Zip File Extraction ", bd=6) # Frame1 on the window/canvas
frame_selectfile.grid(column=0, row=0, padx=10, pady=10, sticky='NSEW') # Positioning frame1
frame_selectfile.configure(borderwidth=1)
self.grid_columnconfigure(0, weight=1) # Configuring the column for the main window/canvas
self.grid_rowconfigure(0, weight=1) # Configuring the row for the main window/canvas
frame_checkpd = tk.LabelFrame(self, text="Step 2: Check PD ", bd=6) # Frame2 on the window/canvas
frame_checkpd.grid(column=1, row=0, padx=10, pady=10, sticky='NSEW') # Positioning frame2
frame_checkpd.configure(borderwidth=1)
self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
frame_festo = tk.LabelFrame(self, text="Festo: ", bd=6) # Frame1 on the window/canvas
frame_festo.grid(column=0, row=1, padx=10, pady=10, sticky='NSEW') # Positioning frame1
frame_festo.configure(borderwidth=1)
self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
frame_transform = tk.LabelFrame(self, text="Transformer: ", bd=6) # Frame1 on the window/canvas
frame_transform.grid(column=1, row=1, padx=10, pady=10, sticky='NSEW') # Positioning frame1
frame_transform.configure(borderwidth=1)
self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
# Initializing all variables in frame_selectfile
file_ID = tk.StringVar()
file_ID2 = tk.StringVar()
location_id = tk.StringVar()
location_id2 = tk.StringVar()
unzip_status = tk.StringVar()
tmpdir = tempfile.TemporaryDirectory().name
#self.create_dir_ifnot_exist(tmpdir)
zip_filename = tk.StringVar()
site_id = tk.StringVar()
site_id.set("0")
data_table_list = []
file_counter = tk.StringVar()
# Initializing all variables in frame_festo
festo_date_id = tk.StringVar()
festo_job_id = tk.StringVar()
festo_engineer_id = tk.StringVar()
festo_station_id = tk.StringVar()
festo_voltage_id = tk.StringVar()
festo_db_id = tk.StringVar()
festo_pd_id = tk.StringVar()
# Initializing all variables in frame_transform
transform_date_id = tk.StringVar()
transform_job_id = tk.StringVar()
transform_engineer_id = tk.StringVar()
transform_station_id = tk.StringVar()
transform_voltage_id = tk.StringVar()
transform_db_id = tk.StringVar()
transform_pd_id = tk.StringVar()
topframe = tk.LabelFrame(frame_festo, bd=0)
topframe_transform = tk.LabelFrame(frame_transform, bd=0)
middleframe = tk.LabelFrame(frame_selectfile, bd=5)
databaseView = ttk.Treeview(middleframe, selectmode="browse")
#label_filename = tk.Label(frame_selectfile, text=" ", width=10, relief='flat').grid(column=0, row=1, padx=5, pady=5, sticky='NW')
label_filelocation = tk.Label(frame_selectfile, text="File Location:", width=12, relief='flat').grid(column=0, row=0, padx=5, pady=5, sticky='NW')
filename_Entry = tk.Label(frame_selectfile, width=52, textvariable=file_ID)
filename_Entry.grid(column=1, row=0, padx=5, pady=5, sticky='NW')
filename_Entry.configure(state='normal')
filelocation_Entry = tk.Entry(frame_selectfile, width=60, textvariable=location_id)
filelocation_Entry.grid(column=1, row=0, padx=5, pady=5, sticky='W')
filelocation_Entry.configure(background='palegreen', foreground='black')
middleframe2 = tk.LabelFrame(frame_festo, bd=5)
databaseView2 = ttk.Treeview(middleframe2, selectmode="browse")
transform_middle_frame = tk.LabelFrame(frame_transform, bd=5)
transformView = ttk.Treeview(transform_middle_frame, selectmode="browse")
#label_filename2 = tk.Label(topframe, text=" ", width=10, relief='flat').grid(column=0, row=1, padx=5, pady=5, sticky='NW')
#label_filelocation2 = tk.Label(topframe, text="File Location:", width=12, relief='flat').grid(column=0, row=2, padx=5, pady=5, sticky='NW')
label_festo_date_id = tk.Label(topframe, text="Scanned Date:", width=12, relief='flat').grid(column=0, row=0, padx=5, pady=5, sticky='NW')
label_festo_job_id = tk.Label(topframe, text="Job No:", width=12, relief='flat').grid(column=2, row=0, padx=5, pady=5, sticky='NW')
label_festo_engineer_id = tk.Label(topframe, text="Engineer:", width=12, relief='flat').grid(column=4, row=0, padx=5, pady=5, sticky='NW')
label_festo_station_id = tk.Label(topframe, text="Station:", width=12, relief='flat').grid(column=0, row=1, padx=5, pady=5, sticky='W')
label_festo_voltage_id = tk.Label(topframe, text="Operating Voltage(kV):", width=18, relief='flat').grid(column=2, row=1, padx=5, pady=5, sticky='NW')
label_festo_db_id = tk.Label(topframe, text="Max dB:", width=12, relief='flat').grid(column=4, row=1, padx=5, pady=5, sticky='NW')
#label_festo_pd_id = tk.Label(topframe, text="Max PD:", width=12, relief='flat').grid(column=0, row=2, padx=5, pady=5, sticky='NW')
festo_date_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_date_id)
festo_date_id_Entry.grid(column=1, row=0, padx=5, pady=5, sticky='NW')
festo_date_id_Entry.configure(state='normal')
festo_job_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_job_id)
festo_job_id_Entry.grid(column=3, row=0, padx=5, pady=5, sticky='NW')
festo_job_id_Entry.configure(state='normal')
festo_engineer_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_engineer_id)
festo_engineer_id_Entry.grid(column=5, row=0, padx=5, pady=5, sticky='NW')
festo_engineer_id_Entry.configure(state='normal')
festo_station_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_station_id)
festo_station_id_Entry.grid(column=1, row=1, padx=5, pady=5, sticky='NW')
festo_station_id_Entry.configure(state='normal')
festo_voltage_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_voltage_id)
festo_voltage_id_Entry.grid(column=3, row=1, padx=5, pady=5, sticky='NW')
festo_voltage_id_Entry.configure(state='normal')
festo_db_id_Entry = tk.Entry(topframe, width=15, textvariable=festo_db_id)
festo_db_id_Entry.grid(column=5, row=1, padx=5, pady=5, sticky='NW')
festo_db_id_Entry.configure(state='normal')
label_transform_date_id = tk.Label(topframe_transform, text="Scanned Date:", width=12, relief='flat').grid(column=0, row=0, padx=5, pady=5, sticky='NW')
label_transform_job_id = tk.Label(topframe_transform, text="Job No:", width=12, relief='flat').grid(column=2, row=0, padx=5, pady=5, sticky='NW')
label_transform_engineer_id = tk.Label(topframe_transform, text="Engineer:", width=12, relief='flat').grid(column=4, row=0, padx=5, pady=5, sticky='NW')
label_transform_station_id = tk.Label(topframe_transform, text="Station:", width=12, relief='flat').grid(column=0, row=1, padx=5, pady=5, sticky='NW')
label_transform_voltage_id = tk.Label(topframe_transform, text="Operating Voltage(kV):", width=18, relief='flat').grid(column=2, row=1, padx=5, pady=5, sticky='NW')
label_transform_db_id = tk.Label(topframe_transform, text="Max dB:", width=12, relief='flat').grid(column=4, row=1, padx=5, pady=5, sticky='NW')
#label_transform_pd_id = tk.Label(topframe, text="Max PD:", width=12, relief='flat').grid(column=0, row=2, padx=5, pady=5, sticky='NW')
transform_date_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_date_id)
transform_date_id_Entry.grid(column=1, row=0, padx=5, pady=5, sticky='NW')
transform_date_id_Entry.configure(state='normal')
transform_job_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_job_id)
transform_job_id_Entry.grid(column=3, row=0, padx=5, pady=5, sticky='NW')
transform_job_id_Entry.configure(state='normal')
transform_engineer_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_engineer_id)
transform_engineer_id_Entry.grid(column=5, row=0, padx=5, pady=5, sticky='NW')
transform_engineer_id_Entry.configure(state='normal')
transform_station_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_station_id)
transform_station_id_Entry.grid(column=1, row=1, padx=5, pady=5, sticky='NW')
transform_station_id_Entry.configure(state='normal')
transform_voltage_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_voltage_id)
transform_voltage_id_Entry.grid(column=3, row=1, padx=5, pady=5, sticky='NW')
transform_voltage_id_Entry.configure(state='normal')
transform_db_id_Entry = tk.Entry(topframe_transform, width=15, textvariable=transform_db_id)
transform_db_id_Entry.grid(column=5, row=1, padx=5, pady=5, sticky='NW')
transform_db_id_Entry.configure(state='normal')
unzip_status.set("")
label_unzipstatus = tk.Label(frame_selectfile, width=20, relief='flat', textvariable=unzip_status)
label_unzipstatus.grid(column=1, row=5, padx=5, pady=5, sticky='NSEW')
selectzip_button = tk.Button(frame_selectfile, width=20, text="Select Zip File", command=lambda: self.upload_action(location_id, zip_filename, tmpdir))
selectzip_button.grid(column=1, row=3, padx=5, pady=3, ipady=3, sticky='SW')
unzip_button = tk.Button(frame_selectfile, width=20, text="Extract", command=lambda: self.extract_nested_zip(databaseView, data_table_list, location_id.get(), tmpdir, zip_filename, site_id, False))
unzip_button.grid(column=1, row=3, padx=5, pady=3, ipady=3, sticky='NE')
# Creating LabelFrame in frame_unzip
upperframe = tk.LabelFrame(frame_selectfile, bd=0)
frame_selectfile.rowconfigure(0, weight=1)
frame_selectfile.columnconfigure(2, weight=1)
upperframe.grid(column=0, row=6, columnspan=2, sticky='W')
# middleframe = tk.LabelFrame(frame_selectfile, bd=5)
middleframe.configure(borderwidth=1)
frame_selectfile.columnconfigure(2, weight=1)
frame_selectfile.rowconfigure(1, weight=1)
middleframe.grid(column=0, row=7, columnspan=2, sticky="NSEW")
middleframe2.configure(borderwidth=1)
frame_festo.columnconfigure(2, weight=1)
frame_festo.rowconfigure(1, weight=1)
middleframe2.grid(column=0, row=1, columnspan=2, sticky="NSEW")
transform_middle_frame.configure(borderwidth=1)
frame_transform.columnconfigure(2, weight=1)
frame_transform.rowconfigure(1, weight=1)
transform_middle_frame.grid(column=0, row=1, columnspan=2, sticky="NSEW")
# lowerframe = tk.LabelFrame(frame_selectfile, bd=0)
# lowerframe.grid(column=0, row=7)
# frame_selectfile.columnconfigure(2, weight=1)
# frame_selectfile.rowconfigure(2, weight=0)
topframe.configure(borderwidth=1)
frame_festo.columnconfigure(2, weight=1)
frame_festo.rowconfigure(1, weight=1)
topframe.grid(column=0, row=0, columnspan=2, sticky="NSEW")
topframe_transform.configure(borderwidth=1)
frame_transform.columnconfigure(2, weight=1)
frame_transform.rowconfigure(1, weight=1)
topframe_transform.grid(column=0, row=0, columnspan=2, sticky="NSEW")
# Labels in frame_unzip
label18 = tk.Label(upperframe, text="Number of sites:", relief='flat')
label18.grid(column=0, row=0, padx=5, pady=5, sticky='W')
site_Entry = tk.Entry(upperframe, width=45, textvariable=site_id)
site_Entry.grid(column=1, row=0, padx=10, pady=5, sticky='E')
site_Entry.configure(state='normal', background='palegreen', foreground='black')
# Button Widgets in frame_unzip
#unzip_btn = tk.Button(upperframe, width=10, text="Unzip File", command=lambda: Unzip(databaseview))
#unzip_btn.grid(column=2, row=0, padx=0, pady=5, ipady=2, sticky='E')
# label_details = tk.Label(upperframe, text=" ", relief='flat')
# label_details.grid(column=0, row=2, padx=5, pady=0, sticky='W')
# Combobox Widgets in frame_unzip
# initializing treeview in frame_unzip
# databaseView = ttk.Treeview(middleframe, selectmode="browse")
databaseView.columnconfigure(2, weight=1)
databaseView.grid(column=0, row=0, columnspan=2, sticky="NSEW")
databaseView2.columnconfigure(2, weight=1)
databaseView2.grid(column=0, row=0, columnspan=2, sticky="NSEW")
transformView.columnconfigure(2, weight=1)
transformView.grid(column=0, row=0, columnspan=2, sticky="NSEW")
# Creating treeview in frame_unzip
vsb = Scrollbar(middleframe, orient="vertical", command=databaseView.yview())
hsb = Scrollbar(middleframe, orient="horizontal")
vsb = Scrollbar(middleframe2, orient="vertical", command=databaseView2.yview())
hsb = Scrollbar(middleframe2, orient="horizontal")
vsb = Scrollbar(transform_middle_frame, orient="vertical", command=transformView.yview())
hsb = Scrollbar(transform_middle_frame, orient="horizontal")
middleframe.columnconfigure(0, weight=1)
middleframe.rowconfigure(0, weight=1)
databaseView["show"] = "headings"
databaseView["columns"] = ("site", "panels", "tevs")
vsb.configure(command=databaseView.yview)
# hsb.configure(command=databaseView.xview)
vsb.grid(column=1, row=0, sticky="NS")
# hsb.grid(column=0, row=1, sticky="WE")
middleframe2.columnconfigure(0, weight=1)
middleframe2.rowconfigure(0, weight=1)
databaseView2["show"] = "headings"
databaseView2["columns"] = ("num", "name", "component", "location", "phase", "db", "prpd", "pulse", "pd")
vsb.configure(command=databaseView2.yview)
# hsb.configure(command=databaseView.xview)
vsb.grid(column=1, row=0, sticky="NS")
transform_middle_frame.columnconfigure(0, weight=1)
transform_middle_frame.rowconfigure(0, weight=1)
transformView["show"] = "headings"
transformView["columns"] = ("num", "name", "component", "location", "phase", "db", "prpd", "pulse", "pd")
vsb.configure(command=transformView.yview)
# hsb.configure(command=databaseView.xview)
vsb.grid(column=1, row=0, sticky="NS")
# Treeview column headings
databaseView.heading("site", text="Site")
databaseView.column("site", anchor='w', width=250)
databaseView.heading("panels", text="Number of Panels")
databaseView.column("panels", anchor='center', width=100)
databaseView.heading("tevs", text="Number of TEVs")
databaseView.column("tevs", anchor='center', width=100)
# Treeview column headings festo
databaseView2.heading("num", text="Panel")
databaseView2.column("num", anchor='w', width=100)
databaseView2.heading("name", text="Tev")
databaseView2.column("name", anchor='center', width=150)
databaseView2.heading("component", text="Component")
databaseView2.column("component", anchor='center', width=150)
databaseView2.heading("location", text="Sublocation")
databaseView2.column("location", anchor='w', width=100)
databaseView2.heading("phase", text="Phase Ref Lock")
databaseView2.column("phase", anchor='center', width=100)
databaseView2.heading("db", text="dB")
databaseView2.column("db", anchor='center', width=100)
databaseView2.heading("prpd", text="PRPD")
databaseView2.column("prpd", anchor='w', width=100)
databaseView2.heading("pulse", text="Pulse Wave")
databaseView2.column("pulse", anchor='center', width=100)
databaseView2.heading("pd", text="PD%")
databaseView2.column("pd", anchor='center', width=100)
# Treeview column headings transform
transformView.heading("num", text="Panel")
transformView.column("num", anchor='w', width=100)
transformView.heading("name", text="Tev")
transformView.column("name", anchor='center', width=150)
transformView.heading("component", text="Component")
transformView.column("component", anchor='center', width=150)
transformView.heading("location", text="Sublocation")
transformView.column("location", anchor='w', width=100)
transformView.heading("phase", text="Phase Ref Lock")
transformView.column("phase", anchor='center', width=100)
transformView.heading("db", text="dB")
transformView.column("db", anchor='center', width=100)
transformView.heading("prpd", text="PRPD")
transformView.column("prpd", anchor='w', width=100)
transformView.heading("pulse", text="Pulse Wave")
transformView.column("pulse", anchor='center', width=100)
transformView.heading("pd", text="PD%")
transformView.column("pd", anchor='center', width=100)
# Widgets in Frame 3
#label_checkstatus = tk.Label(frame_checkpd, text="Status", width=5, relief='flat').grid(column=2, row=0, padx=5, pady=5, sticky='E')
#label_checkstatuscolour = tk.Label(frame_checkpd, text=" ", bg="limegreen", width=5, relief='flat').grid(column=2, row=1, padx=5, pady=5, sticky='E')
findpd_btn = tk.Button(frame_checkpd, width=20, text="Find PDs", command=lambda: self.run_pd_model_exe(tmpdir, zip_filename))
findpd_btn.grid(column=1, row=1, padx=5, pady=5, sticky='E')
export_pdf_btn = tk.Button(frame_checkpd, width=20, text="Export to PDF",
command=lambda: self.export_to_pdf(tmpdir, zip_filename))
export_pdf_btn.grid(column=2, row=1, padx=5, pady=5, sticky='E')
file_counter = 0
find_files_btn = tk.Button(frame_checkpd, width=20, text="Check Files", command=lambda: self.check_files(file_counter, find_files_btn))
find_files_btn.grid(column=3, row=1, padx=5, pady=5, sticky='NSEW')
print(file_counter)
file_counter = 0
#selectzip_button.grid(column=1, row=3, padx=5, pady=3, ipady=3, sticky='SW')
def check_files(self, counter, btn):
# folder path
dir_path = r'C:\Users\Documents\GUI\input_folder'
count = 0
# Iterate directory
for path in os.listdir(dir_path):
# check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
count += 1
print('File count:', count)
return count

Creating GUI desktop program with Python Tkinter on Treeview Why latest data is not shown in another page of Combobox by using postgresql database

I'm creating a GUI desktop program with Python using Tkinter on Treeview that interacts with a Postgresql database. The GUI displays all the contents in Treeview of a certain table when the program launches.
I have 2 tables when I have just added new data to table no.1 with column Id and Article.
Next, I will create new data in table no.2 by using data from table no.1. In this case, I would like to get the latest data but it is not shown in Combobox.
So my problem is why the latest data is not shown how can I fix it.
Here is the code that I need to get the latest data from the first table :
article_combobox = ttk.Combobox(wrapperArtForm, font=('Arial', 13), width=36)
article_combobox.grid(row=1, column=2, sticky="w")
article_combobox['values'] = dbArticle.view_ArticleCol()
Also can see more detail in the image attachment.
enter image description here
Next the second form to input data to the database in the different above table :
class ArticleConditionCreateWindow(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
label = Label(self, text="Article Condition - Create", font=controller.title_font, bg='#99ccff')
label.pack(side="top", fill="x", pady=(5,5))
# Article Condition Create Data
#def article_create():
wrapperArticleCreate = LabelFrame(self, text="Article Condition - Create Data", font=('arial',10,))
wrapperArticleCreate.place(x=130, y=34, width=1040, height=678)
article_create_window = Frame(wrapperArticleCreate, bg="#e6ffe6")#, width=800, height=600)
article_create_window.pack(fill="both", expand="yes", padx=10, pady=10)
# Create Article Condition Form to Enter Article Data
wrapperArtForm = Frame(article_create_window, bg="#e6ffe6", width=1040, height=570)
wrapperArtForm.place(x=0, y=0, width=1040)
def insert_db():
# Check Entry Form must be not Empty
dataEntry = article_combobox.get() and supplier_combobox.get() and article_condition_combobox.get()
if dataEntry !="":
dbArticleCondition.insert(article_combobox.get(), customer_number_entry.get(), description_combobox.get(), supplier_combobox.get(), article_condition_combobox.get(), price_combobox.get(), batch_combobox.get())
def view_da(dbArticleCondition):
trvArtCondition.insert('', 'end', values=dbArticleCondition.view_LastRow())
view_da(dbArticleCondition)
controller.show_frame("ArticleConditionMainWindow")
else:
ttk.tkinter.messagebox.showerror("Article Condition - Create Data","Please Enter Data")
# Clear Text Field
def clear_fields():
id_entry.delete(0, END)
article_combobox.delete(0, END)
customer_number_entry.delete(0, END)
description_combobox.delete(0, END)
supplier_combobox.delete(0, END)
article_condition_combobox.delete(0, END)
price_combobox.delete(0, END)
batch_combobox.delete(0, END)
#Button Control
wrapperBtn = Frame(article_create_window, bg="#e6ffe6", width=1040, height=120)
wrapperBtn.place(x=0 , y=602)
btnSave = Button(wrapperBtn, text="Save", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=insert_db)
btnSave.place(x=590, y=0)
btnClose = Button(wrapperBtn, text="Reset", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=clear_fields)
btnClose.place(x=720, y=0)
btnClose = Button(wrapperBtn, text="Close", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=lambda: controller.show_frame("ArticleConditionMainWindow"))
btnClose.place(x=850, y=0)
# Labels
id_label = Label(wrapperArtForm, text="Id", font=('Prompt', 12)).grid(row=0, column=0, sticky=W, padx=25, pady=8)
article_label = Label(wrapperArtForm, text="Article *", font=('Prompt', 12)).grid(row=1, column=0, sticky=W, padx=25, pady=8)
customer_number_label = Label(wrapperArtForm, text="Customer Number", font=('Prompt', 12)).grid(row=2, column=0, sticky=W, padx=25, pady=8)
description_label = Label(wrapperArtForm, text="Description", font=('Prompt', 12)).grid(row=3, column=0, sticky=W, padx=25, pady=5)
supplier_label = Label(wrapperArtForm, text="Supplier *", font=('Prompt', 12)).grid(row=4, column=0, sticky=W, padx=25, pady=8)
article_condition_label = Label(wrapperArtForm, text="Article Condition *", font=('Prompt', 12)).grid(row=5, column=0, sticky=W, padx=25, pady=8)
price_label = Label(wrapperArtForm, text="Price1", font=('Prompt', 12)).grid(row=6, column=0, sticky=W, padx=25, pady=8)
batch_label = Label(wrapperArtForm, text="Batch No.", font=('Prompt', 12)).grid(row=8, column=0, sticky=W, padx=25, pady=8)
# Entry Boxs
id_entry = ttk.Combobox(wrapperArtForm, font=('Arial', 13), width=5, values=["Auto"], state='readonly')
id_entry.grid(row=0, column=2, sticky=W)
id_entry.current(0)
article_combobox = ttk.Combobox(wrapperArtForm, font=('Arial', 13), width=36)
article_combobox.grid(row=1, column=2, sticky="w")
article_combobox['values'] = dbArticle.view_ArticleCol()
customer_number_entry = Entry(wrapperArtForm, font=('Prompt', 13), width=38)
customer_number_entry.grid(row=2, column=2, sticky="w")
description_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), width=36)
description_combobox.grid(row=3, column=2, sticky="w")
description_combobox['values'] = dbArticle.view_DescriptionCol()
supplier_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), width=36)
supplier_combobox.grid(row=4, column=2, sticky=W)
supplier_combobox['values'] = dbSupplier.view_SupplierCol()
article_condition_combobox = ttk.Combobox(wrapperArtForm, font=('Arial', 13), values=["New","Rework","Used","Dull","Blunt",""], width=10, state='readonly')
article_condition_combobox.grid(row=5, column=2, sticky=W)
#article_condition_combobox.current(0)
price_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), values=["0"], width=10)
price_combobox.grid(row=6, column=2, sticky="w")
price_combobox.current(0)
batch_combobox = Entry(wrapperArtForm, font=('Prompt', 12), width=12)
batch_combobox.grid(row=8, column=2, sticky=W)
Here is the first code of form to input data to database:
class ArticleCreateWindow(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
label = Label(self, text="Article - Create", font=controller.title_font, bg='#99ccff')
label.pack(side="top", fill="x", pady=(5,5))
# Article Create Data
#def article_create():
wrapperArticleCreate = LabelFrame(self, text="Article - Create Data", font=('arial',10,))
wrapperArticleCreate.place(x=130, y=34, width=1040, height=678)
article_create_window = Frame(wrapperArticleCreate, bg="#e6ffe6")#, width=800, height=600)
article_create_window.pack(fill="both", expand="yes", padx=10, pady=10)
# Create Article Form to Enter Article Data
wrapperArtForm = Frame(article_create_window, bg="#e6ffe6", width=1040, height=570)
wrapperArtForm.place(x=0, y=0, width=1040)
def insert_db():
# Check Entry Form must be not Empty
dataEntry = article_entry.get() and description_entry.get() and supplier_combobox.get()
if dataEntry !="":
# Check Duplicate Article Number?
dbArticle.cur.execute("SELECT article FROM article WHERE article=%s", (article_entry.get(),))
data = dbArticle.cur.fetchall()
if not data:
#print ("This Article is NOT Already Exists")
dbArticle.insert(article_entry.get(), customer_number_entry.get(), description_entry.get(), supplier_combobox.get(), price_combobox.get(), unit_combobox.get(), lead_time_entry.get(), batch_combobox.get()
def view_da(dbArticle):
trvArt.insert('', 'end', values=dbArticle.view_LastRow())
view_da(dbArticle)
controller.show_frame("ArticleMainWindow")
else:
#print ("This Article is Already Exists")
ttk.tkinter.messagebox.showerror("Article - Create Data","This Article " + "< " + article_entry.get() + " >" + " has already existed")
#clear_fields()
else:
ttk.tkinter.messagebox.showerror("Article - Create Data","Please Enter Data")
# Clear Text Field
def clear_fields():
id_entry.delete(0, END)
article_entry.delete(0, END)
customer_number_entry.delete(0, END)
description_entry.delete(0, END)
supplier_combobox.delete(0, END)
price_combobox.delete(0, END)
unit_combobox.delete(0, END)
lead_time_entry.delete(0, END)
batch_combobox.delete(0, END)
#Button Control
wrapperBtn = Frame(article_create_window, bg="#e6ffe6", width=1040, height=120)
wrapperBtn.place(x=0 , y=602)
btnSave = Button(wrapperBtn, text="Save", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=insert_db)
btnSave.place(x=590, y=0)
btnClose = Button(wrapperBtn, text="Reset", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=clear_fields)
btnClose.place(x=720, y=0)
btnClose = Button(wrapperBtn, text="Close", bd=1, width=8, height=1, font=('arial',12), fg="#404040", padx=13, pady=3, command=lambda: controller.show_frame("ArticleMainWindow"))
btnClose.place(x=850, y=0)
# Labels
id_label = Label(wrapperArtForm, text="Id", font=('Prompt', 12)).grid(row=0, column=0, sticky=W, padx=25, pady=8)
article_label = Label(wrapperArtForm, text="Article *", font=('Prompt', 12)).grid(row=1, column=0, sticky=W, padx=25, pady=8)
customer_number_label = Label(wrapperArtForm, text="SCI Number", font=('Prompt', 12)).grid(row=2, column=0, sticky=W, padx=25, pady=8)
description_label = Label(wrapperArtForm, text="Description *", font=('Prompt', 12)).grid(row=3, column=0, sticky=W, padx=25, pady=5)
supplier_label = Label(wrapperArtForm, text="Supplier *", font=('Prompt', 12)).grid(row=6, column=0, sticky=W, padx=25, pady=8)
price_label = Label(wrapperArtForm, text="Price *", font=('Prompt', 12)).grid(row=10, column=0, sticky=W, padx=25, pady=8)
unit_label = Label(wrapperArtForm, text="Unit *", font=('Prompt', 12)).grid(row=11, column=0, sticky=W, padx=25, pady=8)
lead_time_label = Label(wrapperArtForm, text="Lead Time *", font=('Prompt', 12)).grid(row=12, column=0, sticky=W, padx=25, pady=8)
batch_label = Label(wrapperArtForm, text="Batch Required *", font=('Prompt', 12)).grid(row=13, column=0, sticky=W, padx=25, pady=8)
# Entry Boxs
id_entry = ttk.Combobox(wrapperArtForm, font=('Arial', 13), width=5, values=["Auto"], state='readonly')
id_entry.grid(row=0, column=2, sticky=W)
id_entry.current(0)
article_entry = Entry(wrapperArtForm, font=('Arial', 13), width=34)
article_entry.grid(row=1, column=2, sticky="w")
customer_number_entry = Entry(wrapperArtForm, font=('Prompt', 13), width=34)
customer_number_entry.grid(row=2, column=2, sticky="w")
description_entry = Entry(wrapperArtForm, font=('Prompt', 13), width=34)
description_entry.grid(row=3, column=2, sticky="w")
supplier_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), width=32)
supplier_combobox.grid(row=6, column=2, sticky=W)
supplier_combobox['values'] = dbSupplier.view_SupplierCol()
price_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), values=["0"], width=10)
price_combobox.grid(row=10, column=2, sticky="w")
price_combobox.current(0)
unit_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 13), values=["Pcs","Set"], width=10)
unit_combobox.grid(row=11, column=2, sticky=W)
unit_combobox.current(0)
lead_time_entry = Spinbox(wrapperArtForm, font=('Arial', 13), from_= 0, to = 20000, width=10)
lead_time_entry.grid(row=12, column=2, sticky=W)
batch_combobox = ttk.Combobox(wrapperArtForm, font=('Prompt', 12), values=['yes', 'no'], width=10)
batch_combobox.grid(row=13, column=2, sticky=W)
batch_combobox.current(0)

Why is the image not showing in the tkinter using if conditions? [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 1 year ago.
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
win=tkinter.Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
def code():
btn1.destroy()
add=StringVar()
sub=StringVar()
pro=StringVar()
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper2, image=img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
def Find():
add.set(float(ent00.get())+float(ent01.get()))
sub.set(float(ent00.get())-float(ent01.get()))
pro.set(float(ent00.get())*float(ent01.get()))
ent00=Entry(wrapper, width=15)
ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
ent01=Entry(wrapper, width=15)
ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')
lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
ent8=Entry(wrapper, textvariable=add, width=15, state='readonly')
ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')
lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
ent15=Entry(wrapper, textvariable=sub, width=15, state='readonly')
ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')
lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
ent9=Entry(wrapper, textvariable=pro, width=15, state='readonly')
ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')
btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
btn.grid(row=11, column=1, padx=20, pady=10)
def img():
if ent8.get()=="4":
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper3, image=img)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
btn2 = Button(wrapper, text = 'Image', command=img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)
btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)
win.geometry("1400x700+250+250")
win.mainloop()
Two images need to be shown on the tkinter. The one defined earlier in wrapper2, shows empty frame while the one that has to appear in wrapper3 after getting 4 as sum, does not appear at all. Moreover, the output printed is "Try again". Why it is so? When sum is 4 it has to show "Move ahead".
First of all, terrible names.
Both your function and your PhotoImage are named img. Rename the function to def add_img().
Second, looking at your code I have no idea what all the wrapper frames are for, why not name them according to what they are planned to hold? Same applies to all the widgets. Wouldn't calc_btn be a better name than btn? img_btn instead of btn2? Why do you need to read more than the name to know what something is?
Third, you have ent8 twice in your code. Once as Label and again as a StringVar.
Tkinter constantly refreshes your window so you need to save the image you are using.
Personally I would have done all of this in a class.
For right now, with your current code, just add
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg")) before your functions and instead of using the variables you are using to open the image, just use Label(wrapper3, image=loaded_img)
As in:
win = Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Edit
Here is the entire code:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
win=Toplevel()
wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)
ent8=StringVar()
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
add_strvar = StringVar()
sub_strvar = StringVar()
pro_strvar = StringVar()
def code():
btn1.destroy()
Label2= Label(wrapper2, image=loaded_img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
def Find():
add_strvar.set(float(ent00.get())+float(ent01.get()))
sub_strvar.set(float(ent00.get())-float(ent01.get()))
pro_strvar.set(float(ent00.get())*float(ent01.get()))
ent00=Entry(wrapper, width=15)
ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
ent01=Entry(wrapper, width=15)
ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')
lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
ent8=Entry(wrapper, textvariable=add_strvar, width=15, state='readonly')
ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')
lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
ent15=Entry(wrapper, textvariable=sub_strvar, width=15, state='readonly')
ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')
lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
ent9=Entry(wrapper, textvariable=pro_strvar, width=15, state='readonly')
ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')
btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
btn.grid(row=11, column=1, padx=20, pady=10)
def add_img():
if add_strvar.get() == "4.0":
Label2= Label(wrapper3, image=loaded_img)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
btn2 = Button(wrapper, text = 'Image', command=add_img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)
btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)
win.geometry("1400x700+250+250")
win.mainloop()
Edit 2
Code changed to work with classes:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image
class ImageCalculator:
def __init__(self, img_path):
self.window = Toplevel()
self.window.geometry("1400x700+250+250")
self.mainframe = Frame(self.window)
self.mainframe.pack(expand=True, fill=BOTH)
self.bg_color = 'crimson'
frame_settings = {'master': self.mainframe, 'bd': 4,
'relief': RIDGE, 'bg': self.bg_color}
frame_names = ('left', 'center', 'right')
self.frames = {name: Frame(**frame_settings) for name in frame_names}
frame_height = 625
init_y = 80
frame_widths = {'left': 465, 'center': 485, 'right': 465}
x = 0
for name in frame_names:
frame_width = frame_widths[name]
self.frames[name].place(x=x, y=init_y, width=frame_width,
height=frame_height)
x += frame_width
self.setup_right_wrapper()
self.code_btn = self.setup_left_wrapper()
self.loaded_image = ImageTk.PhotoImage(Image.open(img_path))
self.add_strvar = StringVar()
self.sub_strvar = StringVar()
self.pro_strvar = StringVar()
def setup_left_wrapper(self) -> Button:
code_btn = Button(self.frames['left'], text='OPEN CODE', command=self.code,
bd='5', width=20, height=2)
img_btn = Button(self.frames['left'], text='Image', bd='5', width=15,
height=2, command=self.add_img)
code_btn.grid(row=11, column=1, padx=20, pady=10)
img_btn.grid(row=12, column=1, padx=20, pady=10)
return code_btn
def setup_right_wrapper(self):
right_frame_title = Label(self.frames['right'], text="Selected Data",
bg=self.bg_color, fg="white",
font=("times new roman",30,"bold"))
right_frame_title.grid(row=0, column=0, padx=20, pady=10)
def code(self):
def Find():
self.add_strvar.set(float(first_entry.get())
+ float(second_entry.get()))
self.sub_strvar.set(float(first_entry.get())
- float(second_entry.get()))
self.pro_strvar.set(float(first_entry.get())
* float(second_entry.get()))
self.code_btn.destroy()
Label2 = Label(self.frames['center'], image=self.loaded_image)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
left_frame = self.frames['left']
first_entry = Entry(left_frame, width=15)
second_entry = Entry(left_frame, width=15)
# Settings of all labels
lbl_settings = {'bg': self.bg_color, 'fg': 'white',
'font': ("times new roman", 15, "bold")}
# Setting of all entry.
entry_settings = {'width': 15, 'state': 'readonly'}
add_lbl = Label(left_frame, text="Add", **lbl_settings)
add_entry = Entry(left_frame, textvariable=self.add_strvar,
**entry_settings)
sub_lbl = Label(left_frame, text="Subtract", **lbl_settings)
sub_entry = Entry(left_frame, textvariable=self.sub_strvar,
**entry_settings)
pro_lbl = Label(left_frame, text="Product", **lbl_settings)
pro_entry = Entry(left_frame, textvariable=self.pro_strvar,
**entry_settings)
calc_btn = Button(left_frame, text='Calculate', command=Find, bd='5',
width=15, height=2)
# Widget placement.
first_entry.grid(row=4, column=1, padx=10, pady=10, sticky='w')
second_entry.grid(row=5, column=1, padx=10, pady=10, sticky='w')
add_lbl.grid(row=6, column=0, padx=20, pady=10, sticky='w')
add_entry.grid(row=6, column=1, padx=10, pady=10, sticky='w')
sub_lbl.grid(row=7, column=0, padx=20, pady=10, sticky='w')
sub_entry.grid(row=7, column=1, padx=10, pady=10, sticky='w')
pro_lbl.grid(row=8, column=0, padx=20, pady=10, sticky='w')
pro_entry.grid(row=8, column=1, padx=10, pady=10, sticky='w')
calc_btn.grid(row=11, column=1, padx=20, pady=10)
def add_img(self):
if self.add_strvar.get() == "4.0":
Label2 = Label(self.frames['right'], image=self.loaded_image)
Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
print("Move ahead")
else:
print("Try again")
def main():
img_calc = ImageCalculator('Amritsar.jpg')
mainloop()
if __name__ == "__main__":
main()
After setting an image for a Label, you need to keep a reference to this image. Otherwise, it's removed at the end of the function and you lose the image (it's garbage collected).
So, when you define your label, just add a line that stores the image as an attribute of the label:
img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
Label2= Label(wrapper2, image=img)
Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
Label2.dontloseit = img
The Label2 widget now has a nonsensical attribute called .dontloseit which holds the image. Now, it won't get collected and it will show in your tkinter widget.
It's one of the peculiarities of tkinter.

tkinter resize GUI on different monitors

I am mainly coding on my MAC.
I created a GUI that is used for a tool I am using to automate things with selenium.
I have used .grid for all widgets in my GUI.
But when I open the GUI on my Windows laptop at home (same resolution but much smaller monitor panel) it is totally messed up.
Here are two screenshot showing the problem,
Messed up layout on Win Laptop (17,3")
The second screenshot shows how it should look.
This is the code I used for the grid layout:
# General GUI settings
app = Tk()
app.title('trade_buddy')
app.geometry('605x800')
app.minsize(605, 800)
app.maxsize(605, 800)
app.grid_rowconfigure(0, weight=1)
app.grid_rowconfigure(1, weight=1)
app.grid_rowconfigure(2, weight=1)
app.grid_rowconfigure(3, weight=1)
app.grid_rowconfigure(4, weight=1)
app.grid_rowconfigure(5, weight=1)
app.grid_rowconfigure(6, weight=1)
app.grid_rowconfigure(7, weight=1)
app.grid_rowconfigure(8, weight=1)
app.grid_rowconfigure(9, weight=1)
app.grid_rowconfigure(10, weight=1)
app.grid_rowconfigure(11, weight=1)
app.grid_rowconfigure(12, weight=1)
app.grid_rowconfigure(13, weight=1)
#Background label
image = Image.open('v2.0/lolo.png')
image = image.resize((600,450), Image.ANTIALIAS)
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(app, image = photo)
label.place(x=0, y=0)
#Buttons
b_init = tk.Button(app,text='Initialize',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10, command=lambda:threading.Thread(target=tb,daemon=True).start())
b_exit = tk.Button(app,text='Exit',font='Tahoma 10 bold',padx=8,bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d', pady=10,command=lambda:threading.Thread(target=exit_tb,daemon=True).start())
b_start = tk.Button(app,text='Start',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=filters,daemon=True).start())
b_pause = tk.Button(app,text='Pause',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=stop_tb,daemon=True).start())
b_tfm = tk.Button(app,text='TFM',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=tfm,daemon=True).start())
#Labels
l_maxBO = tk.Label(app,text='Maximum buy price:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_itemsontf = tk.Label(text='# of items on transfer list:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_speed = tk.Label(text='Choose speed:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
l_routine = tk.Label(text='Choose routine:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
#Entries
e_maxBO = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d', font='Tahoma 10')
e_itemsontf = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
e_fixedsellprice = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
#Text box
t_outputbox = tk.Text(app, width=99, height=27, font='Tahoma 10',bg='#2c2c2c', fg='#f8f09d', relief=SUNKEN, highlightthickness="1")
#Grid Layout
l_maxBO.grid(row=0, column=0, sticky='w', padx=5, pady=5)
l_itemsontf.grid(row=1, column=0, sticky='w', padx=5, pady=5)
l_speed.grid(row=3, column=0, sticky='w', padx=5, pady=1, ipady=1)
l_routine.grid(row=7, column=0, sticky='w', padx=5, pady=1, ipady=1)
e_maxBO.grid(row=0, column=1, sticky='w', padx=5, pady=5)
e_itemsontf.grid(row=1, column=1, sticky='w', padx=5, pady=5)
e_fixedsellprice.grid(row=11, column=0, sticky='w', padx=5, pady=3)
b_exit.grid(row=12, column=8, sticky='sw', padx=5, pady=10)
b_init.grid(row=12, column=4, sticky='sw', padx=5, pady=10)
b_start.grid(row=12, column=0, sticky='sw', padx=5, pady=10)
b_pause.grid(row=12, column=1, sticky='sw', padx=5, pady=10)
b_tfm.grid(row=12, column=2, sticky='sw', padx=5, pady=10)
r_normal.grid(row=4, column=0, sticky='w', padx=5, pady=1)
r_fast.grid(row=5, column=0, sticky='w', padx=5, pady=1)
r_slow.grid(row=6, column=0, sticky='w', padx=5, pady=1)
r_buyonly.grid(row=8, column=0, sticky='w', padx=5, pady=1)
r_fullroutine.grid(row=9, column=0, sticky='w', padx=5, pady=1)
r_buysellfixed.grid(row=10, column=0, sticky='w', padx=5, pady=1)
t_outputbox.grid(row=13, column=0, columnspan=13, rowspan=13, sticky='nsew', padx=5, pady=10)
As a coding beginner, I really have no idea what else I could change.
I already changed from .place to .grid but the problem is still the same.
Does anyone have an idea how I could setup my GUI that it keeps the minimum required geometry relations no matter on what monitor I work?
Here is an example of forcing it to look like the second picture.
import tkinter as tk
from PIL import ImageTk, Image
# General GUI settings
app = tk.Tk()
app.configure(bg="black")
#Background label
image = Image.open('test.png')
image = image.resize((200,200), Image.ANTIALIAS)
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(app, image = photo)
label.grid(row=3, column=1, sticky='w')
#others
r_normal = tk.Radiobutton(app, text="Normal Speed")
r_fast = tk.Radiobutton(app, text="Fast Speed")
r_slow = tk.Radiobutton(app, text="Slow Speed")
r_buyonly = tk.Radiobutton(app, text="Buy only")
r_fullroutine = tk.Radiobutton(app, text="Full routine (optimized price)")
r_buysellfixed = tk.Radiobutton(app, text="Buy and sell for fixed price")
#Buttons
b_init = tk.Button(app,text='Initialize',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10, command=lambda:threading.Thread(target=tb,daemon=True).start())
b_exit = tk.Button(app,text='Exit',font='Tahoma 10 bold',padx=8,bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d', pady=10,command=lambda:threading.Thread(target=exit_tb,daemon=True).start())
b_start = tk.Button(app,text='Start',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=filters,daemon=True).start())
b_pause = tk.Button(app,text='Pause',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=stop_tb,daemon=True).start())
b_tfm = tk.Button(app,text='TFM',font='Tahoma 10 bold',bg='#f8f09d',fg='#1e1e1f',activebackground='#1e1e1f',activeforeground='#f8f09d',padx=8, pady=10,command=lambda:threading.Thread(target=tfm,daemon=True).start())
#Labels
l_maxBO = tk.Label(app,text='Maximum buy price:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_itemsontf = tk.Label(text='# of items on transfer list:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
l_speed = tk.Label(text='Choose speed:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
l_routine = tk.Label(text='Choose routine:',bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10 bold')
#Entries
e_maxBO = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d', font='Tahoma 10')
e_itemsontf = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
e_fixedsellprice = tk.Entry(app, width=10, bg='#1e1e1f', fg='#f8f09d',font='Tahoma 10')
#Text box
t_outputbox = tk.Text(app, width=99, height=27, font='Tahoma 10',bg='#2c2c2c', fg='#f8f09d', relief=tk.SUNKEN, highlightthickness="1")
#Grid Layout
l_maxBO.grid(row=0, column=0, sticky='w', padx=5, pady=5)
l_itemsontf.grid(row=1, column=0, sticky='w', padx=5, pady=5)
l_speed.grid(row=3, column=0, sticky='w', padx=5, pady=1, ipady=1)
l_routine.grid(row=7, column=0, sticky='w', padx=5, pady=1, ipady=1)
e_maxBO.grid(row=0, column=1, sticky='w', padx=5, pady=5)
e_itemsontf.grid(row=1, column=1, sticky='w', padx=5, pady=5)
e_fixedsellprice.grid(row=11, column=0, sticky='w', padx=5, pady=3)
b_exit.grid(row=12, column=8, sticky='sw', padx=5, pady=10)
b_init.grid(row=12, column=4, sticky='sw', padx=5, pady=10)
b_start.grid(row=12, column=0, sticky='sw', padx=5, pady=10)
b_pause.grid(row=12, column=1, sticky='sw', padx=5, pady=10)
b_tfm.grid(row=12, column=2, sticky='sw', padx=5, pady=10)
r_normal.grid(row=4, column=0, sticky='w', padx=5, pady=1)
r_fast.grid(row=5, column=0, sticky='w', padx=5, pady=1)
r_slow.grid(row=6, column=0, sticky='w', padx=5, pady=1)
r_buyonly.grid(row=8, column=0, sticky='w', padx=5, pady=1)
r_fullroutine.grid(row=9, column=0, sticky='w', padx=5, pady=1)
r_buysellfixed.grid(row=10, column=0, sticky='w', padx=5, pady=1)
t_outputbox.grid(row=13, column=0, columnspan=13, rowspan=13, sticky='nsew', padx=5, pady=10)
Notable differences:
I had to set a background color to make up for how we are moving that image.
The image is no longer using .place and instead using .grid.
The image will need to be resized a bit differently because it is a smaller area.
I removed all those row_configures because they were removing some rigidity, you can add it back if you like, but you'll have to re-scale your columns to match the new layout.
Let us know if you have any questions etc.

python: Changing the content of a frame

I started a project where I want to script an application for my company to capture values of an iterative signal processing job.
I want to include the option to switch between different modules that require different parameters to test.
When I click on the buttons on the left I want the content of the data entry frame to change to a different set of Labels and Entry widgets. I tried to put the widgets for 'AAA' in a different class but I don't know how to initialize the class via button click so the widgets in the class get created/visible. I tried it with .lift() but that's ugly. I also want to keep the same 2 frames under the data entry frame, and the ScrolledText on the right, just working with the active module. Any ideas? Someone can push me in the right direction?
Oh yeah, and this is my first project apart from simple read->replace string-> scripts.
Here's the code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
import json
import tkFileDialog
import sys
import ScrolledText
from Tkinter import*
class testlogger(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#--------------------------------------------------------------------------------------
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open", command=self.load_file)
filemenu.add_command(label="Save", command=self.safe_file)
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo")
editmenu.add_separator()
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index")
helpmenu.add_command(label="About...")
menubar.add_cascade(label="Help", menu=helpmenu)
self.config(menu=menubar)
#--------------------------------------------------------------------------------------
treeFrame = tk.Frame(self)
treeFrame.grid(column=0, row=1, sticky='NW')
b1 = Button(treeFrame, text=u'AAA', command=self.AAAlift)
b1.pack(fill='x')
b2 = Button(treeFrame, text=u'RADON', command=self.radonlift)
b2.pack(fill='x')
b3 = Button(treeFrame, text=u'Adaptive Subtract')
b3.pack(fill='x')
b4 = Button(treeFrame, text=u'GMP')
b4.pack(fill='x')
#--------------------------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------------------------
self.enterData = tk.LabelFrame(self, text=" Data Entry ")
self.enterData.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
titleAAA = tk.Label(self.enterData, text="AAA")
titleAAA.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.freqInput = tk.StringVar()
self.freqInput2 = tk.StringVar()
self.thresInput = tk.StringVar()
self.widthInput = tk.StringVar()
self.minFreq = tk.Label(self.enterData, text="Minimum Frequency")
self.minFreq.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minFreqData = tk.Entry(self.enterData, textvariable=self.freqInput)
minFreqData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxFreq = tk.Label(self.enterData, text="Maximum Frequency")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxFreqData = tk.Entry(self.enterData, textvariable=self.freqInput2)
maxFreqData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.threshold = tk.Label(self.enterData, text="Threshold")
self.threshold.grid(row=2, column=3, sticky='E', padx=5, pady=2)
thresData = tk.Entry(self.enterData, textvariable=self.thresInput)
thresData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.width = tk.Label(self.enterData, text="SpatialWidth")
self.width.grid(row=3, column=3, sticky='E', padx=5, pady=2)
widthData = tk.Entry(self.enterData, textvariable=self.widthInput)
widthData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.freqInput, self.freqInput2, self.thresInput, self.widthInput]
self.labelList = [self.minFreq, self.maxFreq, self.threshold, self.width]
enteredAAAData = []
#--------------------------------------------------------------------------------------
self.radon = LabelFrame(self, text=" Data Entry ")
self.radon.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
titleRadon = Label(self.radon, text="Radon Fwd Transform")
titleRadon.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.minMOInput = StringVar()
self.maxMOInput = StringVar()
self.offsetInput = StringVar()
self.numpInput = StringVar()
self.motypeInput = StringVar()
self.maxfreqInput = StringVar()
self.minMO = Label(self.radon, text="Minimum Moveout")
self.minMO.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minMOData = Entry(self.radon, textvariable=self.minMOInput)
minMOData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxMO = Label(self.radon, text="Maximum Moveout")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxMOData = Entry(self.radon, textvariable=self.maxMOInput)
maxMOData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.offset = Label(self.radon, text="Reference Offset")
self.offset.grid(row=2, column=3, sticky='E', padx=5, pady=2)
offsetData = Entry(self.radon, textvariable=self.offsetInput)
offsetData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.numP = Label(self.radon, text="Number of P-Traces")
self.numP.grid(row=3, column=3, sticky='E', padx=5, pady=2)
numPData = Entry(self.radon, textvariable=self.numpInput)
numPData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.motype = Label(self.radon, text="Moveout Type")
self.motype.grid(row=4, column=1, sticky='E', padx=5, pady=2)
motypeData = Entry(self.radon, textvariable=self.motypeInput)
motypeData.grid(row=4, column=2, sticky='E', padx=5, pady=2)
self.maxfreq = Label(self.radon, text="Maximum Frequency")
self.maxfreq.grid(row=4, column=3, sticky='E', padx=5, pady=2)
maxfreqData = Entry(self.radon, textvariable=self.maxfreqInput)
maxfreqData.grid(row=4, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.minMOInput, self.maxMOInput, self.offsetInput, self.numpInput, self.motypeInput, self.maxfreqInput]
self.labelList = [self.minMO, self.maxMO, self.offset, self.numP, self.motype, self.maxfreq]
enteredRadonData = []
#--------------------------------------------------------------------------------------
evalData = tk.Frame(self)
evalData.grid(column=1, row=2, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
evalLabel = tk.Label(evalData, text="Evaluation")
evalLabel.grid(row=1, column=1, columnspan=3, sticky='N', padx=5, pady=2)
self.eval_state = tk.StringVar()
goodRadio = tk.Radiobutton(evalData, text="Good", variable=self.eval_state, value='Good')
goodRadio.grid(row=2, column=1, sticky='W', padx=5, pady=2)
badRadio = tk.Radiobutton(evalData, text="Bad", variable=self.eval_state, value='Bad')
badRadio.grid(row=2, column=2, sticky='W', padx=5, pady=2)
sameRadio = tk.Radiobutton(evalData, text="Same", variable=self.eval_state, value='Same')
sameRadio.grid(row=2, column=3, sticky='W', padx=5, pady=2)
qcDisp = tk.Label(evalData, text="QC Displays:")
qcDisp.grid(row=2,column=4, sticky='W', padx=10, pady=2)
self.checkB4 = tk.IntVar()
optionBefore = tk.Checkbutton(evalData, text="Before", variable=self.checkB4, onvalue=1, offvalue=0)
optionBefore.grid(row=1, column=5, sticky='W', padx=5, pady=2)
optionAfter = tk.Checkbutton(evalData, text="After", onvalue=1, offvalue=0)
optionAfter.grid(row=2, column=5, sticky='W', padx=5, pady=2)
optionDiff = tk.Checkbutton(evalData, text="Difference", onvalue=1, offvalue=0)
optionDiff.grid(row=3, column=5, sticky='W', padx=5, pady=2)
optionSpectrum = tk.Checkbutton(evalData, text="Frequency Spectrum", onvalue=1, offvalue=0)
optionSpectrum.grid(row=1, column=6, sticky='W', padx=5, pady=2)
optionAuto = tk.Checkbutton(evalData, text="Auto Correlation", onvalue=1, offvalue=0)
optionAuto.grid(row=2, column=6, sticky='W', padx=5, pady=2)
optionBanana = tk.Checkbutton(evalData, text="I'm a Banana", onvalue=1, offvalue=0)
optionBanana.grid(row=3, column=6, sticky='W', padx=5, pady=2)
#--------------------------------------------------------------------------------------
self.comment = tk.Text(self, height=5, bg='white')
self.comment.grid(column=1, row=3, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
submit_b = tk.Button(self, text=u"Submit", command=self.enter_data)
submit_b.grid(column=1, row=4, sticky='NE')
#--------------------------------------------------------------------------------------
protocolFrame = tk.LabelFrame(self, text="Protocol")
protocolFrame.grid(row=1, column=2, rowspan=3, sticky='NW')
self.protocolText = ScrolledText.ScrolledText(protocolFrame,state='disabled',height=30, width=70)
self.protocolText.grid(column=0, row=0, sticky='NE')
#--------------------------------------------------------------------------------------
self.L = []
#--------------------------------------------------------------------------------------
def enter_data(self):
x = self.labelList[0].cget('text')
y = self.valueList[0].get()
self.L.append(x + ': ' + y)
x = self.labelList[1].cget('text')
y = self.valueList[1].get()
self.L.append(x + ': ' + y)
x = self.labelList[2].cget('text')
y = self.valueList[2].get()
self.L.append(x + ': ' + y)
x = self.labelList[3].cget('text')
y = self.valueList[3].get()
self.L.append(x + ': ' + y)
x = self.eval_state.get()
self.L.append(x)
if self.checkB4 == 1:
y = self.optionBefore.cget('text')
self.L.append(y)
x = self.comment.get('1.0','end')
self.L.append(x)
self.L.append('-------------------------------------\n')
self.protocolText.config(state='normal')
self.protocolText.insert(tk.END, '\n'.join(self.L))
self.protocolText.config(state='disabled')
self.L = []
#------------------------------------------------------------------------------------------
def safe_file(self):
s = self.protocolText.get('1.0','end')
with open('my_json', 'w') as fp:
json.dump(s, fp)
def load_file(self):
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = '/home'
options['parent'] = self.parent
options['title'] = "Open a file"
self.protocolText.config(state='normal')
with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
for line in f_handle:
self.protocolText.insert(tk.END, f_handle)
self.protocolText.config(state='disabled')
def AAAlift(self):
self.enterData.lift()
def radonlift(self):
self.radon.lift()
if __name__ == "__main__":
app = testlogger(None)
app.title('Testlogger')
app.mainloop()
When app starts:
When AAA is pressed:
I modified your code slightly. Basically I added containerFrame, to wrap the LabelFrames. I think its easier to make it easer this way. I also added some padding in other places to make the Protocol part inline with others.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
import json
import tkFileDialog
import sys
import ScrolledText
from Tkinter import*
class testlogger(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
#--------------------------------------------------------------------------------------
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open", command=self.load_file)
filemenu.add_command(label="Save", command=self.safe_file)
filemenu.add_command(label="Save as...")
filemenu.add_command(label="Close")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo")
editmenu.add_separator()
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index")
helpmenu.add_command(label="About...")
menubar.add_cascade(label="Help", menu=helpmenu)
self.config(menu=menubar)
#--------------------------------------------------------------------------------------
treeFrame = tk.Frame(self)
treeFrame.grid(column=0, row=1, sticky='NW')
b1 = Button(treeFrame, text=u'AAA', command=self.AAAlift)
b1.pack(fill='x')
b2 = Button(treeFrame, text=u'RADON', command=self.radonlift)
b2.pack(fill='x')
b3 = Button(treeFrame, text=u'Adaptive Subtract')
b3.pack(fill='x')
b4 = Button(treeFrame, text=u'GMP')
b4.pack(fill='x')
#--------------------------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------------------------
self.containerFrame = tk.Frame(self)
self.containerFrame.grid(column=1, row=1, columnspan=4, sticky='NW', \
padx=5, pady=5, ipadx=1, ipady=1)
self.enterData = tk.LabelFrame(self.containerFrame, text=" Data Entry ")
#self.enterData.pack(fill=tk.BOTH, expand=1)
self.enterData.grid(column=0, row=0, sticky='NW', ipadx=17)
# self.enterData.grid(column=1, row=1, columnspan=4, sticky='NW', \
# padx=5, pady=5, ipadx=5, ipady=5)
titleAAA = tk.Label(self.enterData, text="AAA")
titleAAA.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.freqInput = tk.StringVar()
self.freqInput2 = tk.StringVar()
self.thresInput = tk.StringVar()
self.widthInput = tk.StringVar()
self.minFreq = tk.Label(self.enterData, text="Minimum Frequency")
self.minFreq.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minFreqData = tk.Entry(self.enterData, textvariable=self.freqInput)
minFreqData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxFreq = tk.Label(self.enterData, text="Maximum Frequency")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxFreqData = tk.Entry(self.enterData, textvariable=self.freqInput2)
maxFreqData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.threshold = tk.Label(self.enterData, text="Threshold")
self.threshold.grid(row=2, column=3, sticky='E', padx=5, pady=2)
thresData = tk.Entry(self.enterData, textvariable=self.thresInput)
thresData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.width = tk.Label(self.enterData, text="SpatialWidth")
self.width.grid(row=3, column=3, sticky='E', padx=5, pady=2)
widthData = tk.Entry(self.enterData, textvariable=self.widthInput)
widthData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.freqInput, self.freqInput2, self.thresInput, self.widthInput]
self.labelList = [self.minFreq, self.maxFreq, self.threshold, self.width]
enteredAAAData = []
#--------------------------------------------------------------------------------------
self.radon = LabelFrame(self.containerFrame, text=" Data Entry ")
#self.radon.pack(fill=tk.BOTH, expand=1)
self.radon.grid(column=0, row=0, sticky='NW', ipadx=0)
titleRadon = Label(self.radon, text="Radon Fwd Transform")
titleRadon.grid(row=1, columnspan=4, sticky='N', padx=5, pady=2)
self.minMOInput = StringVar()
self.maxMOInput = StringVar()
self.offsetInput = StringVar()
self.numpInput = StringVar()
self.motypeInput = StringVar()
self.maxfreqInput = StringVar()
self.minMO = Label(self.radon, text="Minimum Moveout")
self.minMO.grid(row=2, column=1, sticky='E', padx=5, pady=2)
minMOData = Entry(self.radon, textvariable=self.minMOInput)
minMOData.grid(row=2, column=2, sticky='E', padx=5, pady=2)
self.maxMO = Label(self.radon, text="Maximum Moveout")
self.maxFreq.grid(row=3, column=1, sticky='E', padx=5, pady=2)
maxMOData = Entry(self.radon, textvariable=self.maxMOInput)
maxMOData.grid(row=3, column=2, sticky='E', padx=5, pady=2)
self.offset = Label(self.radon, text="Reference Offset")
self.offset.grid(row=2, column=3, sticky='E', padx=5, pady=2)
offsetData = Entry(self.radon, textvariable=self.offsetInput)
offsetData.grid(row=2, column=4, sticky='E', padx=5, pady=2)
self.numP = Label(self.radon, text="Number of P-Traces")
self.numP.grid(row=3, column=3, sticky='E', padx=5, pady=2)
numPData = Entry(self.radon, textvariable=self.numpInput)
numPData.grid(row=3, column=4, sticky='E', padx=5, pady=2)
self.motype = Label(self.radon, text="Moveout Type")
self.motype.grid(row=4, column=1, sticky='E', padx=5, pady=2)
motypeData = Entry(self.radon, textvariable=self.motypeInput)
motypeData.grid(row=4, column=2, sticky='E', padx=5, pady=2)
self.maxfreq = Label(self.radon, text="Maximum Frequency")
self.maxfreq.grid(row=4, column=3, sticky='E', padx=5, pady=2)
maxfreqData = Entry(self.radon, textvariable=self.maxfreqInput)
maxfreqData.grid(row=4, column=4, sticky='E', padx=5, pady=2)
self.valueList = [self.minMOInput, self.maxMOInput, self.offsetInput, self.numpInput, self.motypeInput, self.maxfreqInput]
self.labelList = [self.minMO, self.maxMO, self.offset, self.numP, self.motype, self.maxfreq]
enteredRadonData = []
#--------------------------------------------------------------------------------------
evalData = tk.Frame(self)
evalData.grid(column=1, row=2, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
evalLabel = tk.Label(evalData, text="Evaluation")
evalLabel.grid(row=1, column=1, columnspan=3, sticky='N', padx=5, pady=2)
self.eval_state = tk.StringVar()
goodRadio = tk.Radiobutton(evalData, text="Good", variable=self.eval_state, value='Good')
goodRadio.grid(row=2, column=1, sticky='W', padx=5, pady=2)
badRadio = tk.Radiobutton(evalData, text="Bad", variable=self.eval_state, value='Bad')
badRadio.grid(row=2, column=2, sticky='W', padx=5, pady=2)
sameRadio = tk.Radiobutton(evalData, text="Same", variable=self.eval_state, value='Same')
sameRadio.grid(row=2, column=3, sticky='W', padx=5, pady=2)
qcDisp = tk.Label(evalData, text="QC Displays:")
qcDisp.grid(row=2,column=4, sticky='W', padx=10, pady=2)
self.checkB4 = tk.IntVar()
optionBefore = tk.Checkbutton(evalData, text="Before", variable=self.checkB4, onvalue=1, offvalue=0)
optionBefore.grid(row=1, column=5, sticky='W', padx=5, pady=2)
optionAfter = tk.Checkbutton(evalData, text="After", onvalue=1, offvalue=0)
optionAfter.grid(row=2, column=5, sticky='W', padx=5, pady=2)
optionDiff = tk.Checkbutton(evalData, text="Difference", onvalue=1, offvalue=0)
optionDiff.grid(row=3, column=5, sticky='W', padx=5, pady=2)
optionSpectrum = tk.Checkbutton(evalData, text="Frequency Spectrum", onvalue=1, offvalue=0)
optionSpectrum.grid(row=1, column=6, sticky='W', padx=5, pady=2)
optionAuto = tk.Checkbutton(evalData, text="Auto Correlation", onvalue=1, offvalue=0)
optionAuto.grid(row=2, column=6, sticky='W', padx=5, pady=2)
optionBanana = tk.Checkbutton(evalData, text="I'm a Banana", onvalue=1, offvalue=0)
optionBanana.grid(row=3, column=6, sticky='W', padx=5, pady=2)
#--------------------------------------------------------------------------------------
self.comment = tk.Text(self, height=5, bg='white')
self.comment.grid(column=1, row=3, sticky='NW', \
padx=5, pady=5, ipadx=5, ipady=5)
submit_b = tk.Button(self, text=u"Submit", command=self.enter_data)
submit_b.grid(column=1, row=4, sticky='NE')
#--------------------------------------------------------------------------------------
protocolFrame = tk.LabelFrame(self, text="Protocol")
protocolFrame.grid(row=1, column=2, padx=45, pady=5, rowspan=3, sticky='NW')
self.protocolText = ScrolledText.ScrolledText(protocolFrame,state='disabled',height=30, width=70)
self.protocolText.grid(column=0, row=0, sticky='NE')
#--------------------------------------------------------------------------------------
self.L = []
#--------------------------------------------------------------------------------------
def enter_data(self):
x = self.labelList[0].cget('text')
y = self.valueList[0].get()
self.L.append(x + ': ' + y)
x = self.labelList[1].cget('text')
y = self.valueList[1].get()
self.L.append(x + ': ' + y)
x = self.labelList[2].cget('text')
y = self.valueList[2].get()
self.L.append(x + ': ' + y)
x = self.labelList[3].cget('text')
y = self.valueList[3].get()
self.L.append(x + ': ' + y)
x = self.eval_state.get()
self.L.append(x)
if self.checkB4 == 1:
y = self.optionBefore.cget('text')
self.L.append(y)
x = self.comment.get('1.0','end')
self.L.append(x)
self.L.append('-------------------------------------\n')
self.protocolText.config(state='normal')
self.protocolText.insert(tk.END, '\n'.join(self.L))
self.protocolText.config(state='disabled')
self.L = []
#------------------------------------------------------------------------------------------
def safe_file(self):
s = self.protocolText.get('1.0','end')
with open('my_json', 'w') as fp:
json.dump(s, fp)
def load_file(self):
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = '/home'
options['parent'] = self.parent
options['title'] = "Open a file"
self.protocolText.config(state='normal')
with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
for line in f_handle:
self.protocolText.insert(tk.END, f_handle)
self.protocolText.config(state='disabled')
def AAAlift(self):
#self.enterData.grid_forget()
self.radon.grid_remove()
self.enterData.grid()
def radonlift(self):
self.enterData.grid_remove()
self.radon.grid()
if __name__ == "__main__":
app = testlogger(None)
app.title('Testlogger')
app.mainloop()
The screenshots are:
Obviously more work is needed to make everything "even" and inline with each other, but pressing AAA and RADON buttons works as expected (I think). You can expand/amend on my modifications, and hopefully it will allow you to achieve desired effect.

Categories

Resources