Related
I'm making a game based off of the periodic table with tkinter. I made the particle frame just fine, so I decided to copy the code and reuse it for the element frame, changing only the variable names. But for some reason, even though the particle frame works just fine, nothing shows up for the element frame. Here is my full code:
# Boilerplate
import random
import periodictable as pt
from tkinter import *
root = Tk()
root.title('Periodic Table Game')
root.geometry('350x250')
LightBlue = "#b3c7d6"
Menu = Frame(root)
elementFrame = Frame(root)
particleFrame = Frame(root)
settingsFrame = Frame(root)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
for AllFrames in (Menu, elementFrame, particleFrame, settingsFrame):
AllFrames.grid(row=0, column=0, sticky='nsew')
AllFrames.configure(bg=LightBlue)
def show_frame(frame):
frame.tkraise()
show_frame(Menu)
# Menu Frame
Menu.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
MenuTitle = Label(Menu, text="Periodic Table Game", font=("Arial", 15), bg=LightBlue)
MenuTitle.grid(row=0, column=0, pady=25)
MenuTitle.grid_rowconfigure(1, weight=1)
MenuTitle.grid_columnconfigure(1, weight=1)
MenuButton1 = Button(Menu, width=25, text="Guess The Particles", command=lambda: show_frame(particleFrame))
MenuButton1.grid(row=1, column=0)
MenuButton2 = Button(Menu, width=25, text="Guess The Element Name", command=lambda: show_frame(elementFrame))
MenuButton2.grid(row=2, column=0, pady=5)
SettingsButton = Button(Menu, width=25, text="Settings", command=lambda: show_frame(settingsFrame))
SettingsButton.grid(row=3, column=0)
# Particle Frame
particleFrame.grid_columnconfigure(0, weight=1)
BackButtonF2 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF2.grid(row=0, column=0, sticky=W)
ParticleLabel = Label(particleFrame, text='testing', bg=LightBlue)
ParticleLabel.grid(row=1, column=0, pady=15)
ParticleEntry = Entry(particleFrame)
ParticleEntry.grid(row=2, column=0, pady=10)
ParticleEnter = Button(particleFrame, text='Enter', width=10)
ParticleEnter.grid(row=3, column=0, pady=10)
# Element Frame
elementFrame.grid_columnconfigure(0, weight=1)
BackButtonF3 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF3.grid(row=0, column=0, sticky=W)
ElementLabel = Label(particleFrame, text='testing', bg=LightBlue)
ElementLabel.grid(row=1, column=0, pady=15)
ElementEntry = Entry(particleFrame)
ElementEntry.grid(row=2, column=0, pady=10)
ElementEnter = Button(particleFrame, text='Enter', width=10)
ElementEnter.grid(row=3, column=0, pady=10)
root.mainloop()
Why does identical code work only with one frame?
Precisely, because you copied the code you don't spot where the issue is.
When you are defining the element frame widgets you are placing them all into particleFrame.
Example:
BackButtonF3 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))
should be
BackButtonF3 = Button(elementFrame, text='Back', command=lambda: show_frame(Menu))
Your problem will be solved.
Change this particleFrame, to elementFrame
snippet code:
BackButtonF3 = Button(elementFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF3.grid(row=0, column=0, sticky=W)
ElementLabel = Label(elementFrame, text='testing', bg=LightBlue)
ElementLabel.grid(row=1, column=0, pady=15)
ElementEntry = Entry(elementFrame)
ElementEntry.grid(row=2, column=0, pady=10)
ElementEnter = Button(elementFrame, text='Enter', width=10)
ElementEnter.grid(row=3, column=0, pady=10)
Screenshot before:
Screenshot after same as elementFrame and particleFrame:
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
The following code
root = tk.Tk()
frame = ttk.Frame(root, padding=10)
frame.grid()
# Browse
tk.Text(frame, state="disabled", height=1).grid(column=0, row=0, columnspan=3, sticky="W", pady=5, ipadx=2, ipady=2)
ttk.Button(frame, text="Browse", command=None).grid(column=3, row=0, sticky="E")
# Selected files
tk.Text(frame, state="disabled").grid(column=0, row=1, columnspan=4, sticky="EW", pady=10)
# Options
ttk.Button(frame, text="SELECT", command=None).grid(column=0, row=2, sticky="W")
ttk.Button(frame, text="DELETE", command=None).grid(column=1, row=2, sticky="W")
ttk.Button(frame, text="Merge", command=None).grid(column=2, row=2, sticky="W")
ttk.Button(frame, text="Close", command=root.destroy).grid(column=3, row=2, sticky="E")
root.mainloop()
produces this,
but I want the DELETE and MERGE buttons to be left-aligned so they are together with the SELECT button. How can I achieve this?
Put buttons in new Frame and this frame put in original Frame
buttons_frame = ttk.Frame(frame)
buttons_frame.grid(column=0, row=2, sticky="W")
ttk.Button(buttons_frame, text="SELECT", command=None).grid(column=0, row=2, sticky="W")
ttk.Button(buttons_frame, text="DELETE", command=None).grid(column=1, row=2, sticky="W")
ttk.Button(buttons_frame, text="Merge", command=None).grid(column=2, row=2, sticky="W")
BTW:
In buttons_frame you may even use .pack(side='left') instead of grid()
Full code:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
frame = ttk.Frame(root, padding=10)
frame.grid()
# Browse
tk.Text(frame, state="disabled", height=1).grid(column=0, row=0, columnspan=3, sticky="W", pady=5, ipadx=2, ipady=2)
ttk.Button(frame, text="Browse", command=None).grid(column=3, row=0, sticky="E")
# Selected files
tk.Text(frame, state="disabled").grid(column=0, row=1, columnspan=4, sticky="EW", pady=10)
# Options
buttons_frame = ttk.Frame(frame)
buttons_frame.grid(column=0, row=2, sticky="W")
ttk.Button(buttons_frame, text="SELECT", command=None).grid(column=0, row=2, sticky="W")
ttk.Button(buttons_frame, text="DELETE", command=None).grid(column=1, row=2, sticky="W")
ttk.Button(buttons_frame, text="Merge", command=None).grid(column=2, row=2, sticky="W")
ttk.Button(frame, text="Close", command=root.destroy).grid(column=3, row=2, sticky="E")
root.mainloop()
EDIT:
You may also add empy column between Merge and Close and assign weight=1 to this column so it will get all empty space.
It needs to move Browse and Close to next column, and increase columnspan in both Text
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
frame = ttk.Frame(root, padding=10)
frame.grid()
frame.columnconfigure(3, weight=1) # column 3 will use all free space
# Browse
tk.Text(frame, state="disabled", height=1).grid(column=0, row=0, columnspan=4, sticky="W", pady=5, ipadx=2, ipady=2)
ttk.Button(frame, text="Browse", command=None).grid(column=4, row=0, sticky="E")
# Selected files
tk.Text(frame, state="disabled").grid(column=0, row=1, columnspan=5, sticky="EW", pady=10)
# Options
ttk.Button(frame, text="SELECT", command=None).grid(column=0, row=2, sticky="W")
ttk.Button(frame, text="DELETE", command=None).grid(column=1, row=2, sticky="W")
ttk.Button(frame, text="Merge", command=None).grid(column=2, row=2, sticky="W")
ttk.Button(frame, text="Close", command=root.destroy).grid(column=4, row=2, sticky="E")
root.mainloop()
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.
I have a grid placing a top, center, and footer frames, then I'm making frames inside the Center frame (I'll call them subframes as I don't know what to refer to them as). When I try to pack/grid/place a button inside the subframes it won't work. If I grid the buttons, they appear at the bottom of all top-level frames which doesn't make any sense to me since I put them in their appropriate subframes... This is really hard to explain, but that's the best I can do.
from tkinter import messagebox
import math
# gui
root = tk.Tk()
root.title('Bid Generator')
root.geometry('{}x{}'.format(800, 880))
# main containers
top_frame = tk.Frame(root, bg='#00c3ff', height=75)
center = tk.Frame(root, bg='white')
footer = tk.Frame(root, bg='black', height=100)
# layout of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
footer.grid(row=2, sticky="ew")
# distributing equal weight (priority) to center frame columns
center.grid_columnconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)
center.grid_columnconfigure(2, weight=1)
center.grid_rowconfigure(0, weight=1)
center.grid_rowconfigure(1, weight=1)
center.grid_rowconfigure(2, weight=1)
center.grid_rowconfigure(3, weight=1)
center.grid_rowconfigure(4, weight=1)
center.grid_rowconfigure(5, weight=1)
center.grid_rowconfigure(6, weight=1)
center.grid_rowconfigure(7, weight=1)
center.grid_rowconfigure(8, weight=1)
center.grid_rowconfigure(9, weight=1)
center.grid_rowconfigure(10, weight=1)
center.grid_rowconfigure(11, weight=1)
center.grid_rowconfigure(12, weight=1)
# label widget for the top frame
aibs_label = tk.Label(top_frame, text='Bid Generator', bg='#00c3ff').pack()
# Supported bank frames
bank_label = tk.Label(center, text='Supported Bank?', bg="gray", height=20).grid(row=0, columnspan=3, sticky="ew")
bank_left = tk.Frame(center, bg="red", padx=10, pady=10, height=10, width=10).grid(row=1, column=0, sticky="ew")
bank_mid = tk.Frame(center, bg="green", padx=10, pady=10, height=10, width=10).grid(row=1, column=1, sticky="ew")
bank_right = tk.Frame(center, bg="blue", padx=10, pady=10, height=10, width=10).grid(row=1, column=2, sticky="ew")
bank_yes = tk.Button(bank_left, text="Yes", width=24, height=3).grid()
bank_no = tk.Button(bank_mid, text="No", width=24, height=3).grid()
bank_name = tk.Button(bank_right, text="entry", width=24, height=3).grid()
# credit cards
cc_label = tk.Label(center, text='Credit cards?', bg="gray", height=20).grid(row=1, columnspan=3, sticky="ew")
cc_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=2, column=0, sticky="ew")
cc_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=2, column=1, sticky="ew")
cc_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=2, column=2, sticky="ew")
# checks frames
chks_label = tk.Label(center, text='Checks?', bg="gray", height=20).grid(row=3, columnspan=3, sticky="ew")
chks_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=4, column=0, sticky="ew")
chks_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=4, column=1, sticky="ew")
chks_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=4, column=2, sticky="ew")
# payroll frames
pr_label = tk.Label(center, text='Payroll?', bg="gray", height=20).grid(row=5, columnspan=3, sticky="ew")
pr_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=6, column=0, sticky="ew")
pr_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=6, column=1, sticky="ew")
pr_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=6, column=2, sticky="ew")
# sales tax frames
st_label = tk.Label(center, text='Sales Tax?', bg="gray", height=20).grid(row=7, columnspan=3, sticky="ew")
st_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=8, column=0, sticky="ew")
st_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=8, column=1, sticky="ew")
st_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=8, column=2, sticky="ew")
# workers comp frames
wc_label = tk.Label(center, text='Workers Comp?', bg="gray", height=20).grid(row=9, columnspan=3, sticky="ew")
wc_left = tk.Frame(center, bg="red", height=10, width=10).grid(row=10, column=0, sticky="ew")
wc_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=10, column=1, sticky="ew")
wc_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=10, column=2, sticky="ew")
# qbox frames
qbox_label = tk.Label(center, text='Qbox?', bg="gray", height=20).grid(row=11, columnspan=3, sticky="ew")
qbox_left = tk.Frame(center, bg="red", height=100, width=10).grid(row=12, column=0, sticky="ew")
qbox_mid = tk.Frame(center, bg="green", height=10, width=10).grid(row=12, column=1, sticky="ew")
qbox_right = tk.Frame(center, bg="blue", height=10, width=10).grid(row=12, column=2, sticky="ew")
# End of line
root.mainloop()