I'm placing a scrollbar in a frame, and the frame in a widget. The frame has a label above it. The label above has three columns. The frame with the scrollbar has three columns. I can't get the three columns in the frame and above the frame to line up.
I saw and used the following two questions to get this far, but got stuck.
Adding a scrollbar to a group of widgets in Tkinter
Tkinter - Add scrollbar for each LabelFrame
Any help on lining up the columns would be greatly appreciated. Thanks.
Here's a picture of the widget without the lined up columns:
Columns don't line up
Here's a MWE:
import tkinter as tk
class Selections(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.FifthLabelLeft = tk.Label(self,
text="""Riding""",
justify = tk.CENTER,
padx = 10).grid(row=4, column = 0, pady=5)
self.FifthLabelCenter = tk.Label(self,
text="""Winning Candidate""",
justify = tk.CENTER,
padx = 10).grid(row=4, column = 1, pady=5)
self.FifthLabelRight = tk.Label(self,
text="""Percent of Vote""",
justify = tk.CENTER,
padx = 10).grid(row=4, column = 2, pady=5)
mybox = tk.LabelFrame(self, padx=5, pady=4)
mybox.grid(row=5, column=0, columnspan=3)
canvas = tk.Canvas(mybox, borderwidth=5, background="#70ff33")
frame = tk.Frame(canvas, background="#33f4ff")
vsb = tk.Scrollbar(mybox, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set, width=450, heigh=50)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")
# be sure that we call OnFrameConfigure on the right canvas
frame.bind("<Configure>", lambda event: self.OnFrameConfigure(canvas))
self.fillWindow(frame)
self.QuitButton = tk.Button(self,
text="QUIT",
command=root.destroy,
padx=25, pady=0)
self.QuitButton.grid(column = 0, columnspan=3)
def fillWindow(self, frame):
PartyWinnersList = [['Some list of places', "Somebody's Name", 0.37448599960838064],
['A shorter list', 'Three Long Names Here', 0.52167817821240514],
['A much longer, longer entry', 'Short Name', 0.41945832387008858]]
placement = 2
for i in PartyWinnersList:
ShowYear = tk.Label(frame,
text="""%s """ % i[0]
)
ShowYear.grid(row=placement, column = 0, sticky=tk.S)
ShowSystem = tk.Label(frame,
text="""%s """ % i[1]
)
ShowSystem.grid(row=placement, column = 1, sticky=tk.N)
PercentVotes = i[2]*100
ShowVotes = tk.Label(frame,
text="""%3.1f""" % PercentVotes
)
ShowVotes.grid(row=placement, column = 2, sticky=tk.N)
placement += 1
def OnFrameConfigure(self, canvas):
canvas.configure(scrollregion=canvas.bbox("all"))
if __name__ == "__main__":
root = tk.Tk()
main = Selections(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
The two defined functions are in the class, but I couldn't get them to line up.
I usually don't solve the problem this way, but you can force alignment by specifying label widths. The problem will come when you dynamically resize the window -- everything is static. But this will solve the specified problem -- alignment.
import tkinter as tk
class Selections(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.FifthLabelLeft = tk.Label(self,
text="""Riding""",
justify = tk.CENTER,
width=25,padx = 10)
self.FifthLabelLeft.grid(row=4, column = 0, pady=5)
self.FifthLabelCenter = tk.Label(self,
text="""Winning Candidate""",
justify = tk.CENTER,
width=25,
padx = 10).grid(row=4, column = 1, pady=5)
self.FifthLabelRight = tk.Label(self,
text="""Percent of Vote""",
justify = tk.CENTER,
padx = 10).grid(row=4, column = 2, pady=5)
mybox = tk.LabelFrame(self, padx=5, pady=4)
mybox.grid(row=5, column=0, columnspan=3)
canvas = tk.Canvas(mybox, borderwidth=5, background="#70ff33")
frame = tk.Frame(canvas, background="#33f4ff")
# frame.grid_columnconfigure((0,1,2), weight=3)
vsb = tk.Scrollbar(mybox, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set, width=450, heigh=50)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")
# be sure that we call OnFrameConfigure on the right canvas
frame.bind("<Configure>", lambda event: self.OnFrameConfigure(canvas))
self.fillWindow(frame)
self.QuitButton = tk.Button(self,
text="QUIT",
command=root.destroy,
padx=25, pady=0)
self.QuitButton.grid(column = 0, columnspan=3)
def fillWindow(self, frame):
PartyWinnersList = [['Some list of places', "Somebody's Name", 0.37448599960838064],
['A shorter list', 'Three Long Names Here', 0.52167817821240514],
['A much longer, longer entry', 'Short Name', 0.41945832387008858]]
placement = 2
for i in PartyWinnersList:
ShowYear = tk.Label(frame, text="""%s """ % i[0], width=25)
ShowYear.grid(row=placement, column = 0, sticky=tk.S)
ShowSystem = tk.Label(frame,
text="""%s """ % i[1],
width=25
)
ShowSystem.grid(row=placement, column = 1, sticky=tk.N)
PercentVotes = i[2]*100
ShowVotes = tk.Label(frame,
text="""%3.1f""" % PercentVotes,
width=15
)
ShowVotes.grid(row=placement, column = 2, sticky=tk.N)
placement += 1
def OnFrameConfigure(self, canvas):
canvas.configure(scrollregion=canvas.bbox("all"))
if __name__ == "__main__":
root = tk.Tk()
main = Selections(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
Related
I am working on a little finance project. When I add to my balance, the entry gets listed among themselves. But I want to "change direction", so the newest entry is always on row 3 and the others lists among themselves. The problem is I don't know how to access to the other Elements to add to their row.
It should be something like that:
I entry a amount to add to the main balance- for example 40$
It gets shown on column 0, row 3
If I add another amount it should show on row 3- for example 20$, and the 40$ should move to row 4.
It should get listed like that:
row 3: 20$ ,
row 4: 40$
and so on..
Sorry for beginner mistakes, Im new to python :)
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class App():
def __init__(self):
self.window = tk.Tk()
self.window.title("Expenses")
self.window.geometry("700x600")
self.window.resizable(0,0)
self.username_list = ["a"]
self.password_list = ["a"]
self.balace = 90
self.row_first_column = 2
self.start_frame()
self.window.mainloop()
def start_frame(self):
self.window.columnconfigure(0, weight=1)
self.window.columnconfigure(1, weight=1)
self.window.columnconfigure(2, weight=1)
self.heading_label = ttk.Label(self.window, text="Log-In", font=("", 25))
self.heading_label.grid(column=1, row=0, sticky=tk.NS, pady=40)
self.username = ttk.Label(self.window, text="Username:")
self.username.grid(column=1, row=1, sticky=tk.W, pady=30)
self.password = ttk.Label(self.window, text="Password:")
self.password.grid(column=1, row=2, sticky=tk.W)
self.entry1 = ttk.Entry(self.window)
self.entry1.grid(column=1, row=1)
self.entry2 = ttk.Entry(self.window)
self.entry2.grid(column=1, row=2)
self.warning = ttk.Label(self.window, text="")
self.warning.grid(column=1, row=3, sticky= tk.NS, pady=10)
self.button1 = ttk.Button(self.window, text="Submit", command= self.check_data)
self.button1.grid(column=1, row=4, sticky=tk.NS)
def check_data(self):
username_check = self.entry1.get()
password_check = self.entry2.get()
if username_check in self.username_list and password_check in self.password_list:
self.main_frame()
else:
self.warning.configure(text="Incorrect data! Try again")
self.entry2.delete(0, tk.END)
def main_frame(self):
self.username.destroy()
self.password.destroy()
self.entry1.destroy()
self.entry2.destroy()
self.warning.destroy()
self.button1.destroy()
self.heading_label.configure(text="Finance", font=("", 25))
#First Column
self.sub_heading1 = ttk.Label(self.window, text="Overview", font=("", 15))
self.sub_heading1.grid(column=0, sticky=tk.NS, row=1, pady=20)
self.balance_label = ttk.Label(self.window, text=f"{self.balace}$")
self.balance_label.grid(column=0, row=2, pady=20, sticky=tk.NS)
#Second Column
self.sub_heading2 = ttk.Label(self.window, text="Add", font=("", 15))
self.sub_heading2.grid(column=1, sticky=tk.NS, row=1, pady=20)
self.add_balance = ttk.Entry(self.window, width=5)
self.add_balance.grid(column=1, row=2, pady=20, sticky=tk.NS)
self.add_button = ttk.Button(self.window, text="Add",width=3, command= self.entry_add_balance)
self.add_button.grid(column=1, row=3, pady=10, sticky=tk.NS)
#Third Column
self.sub_heading3 = ttk.Label(self.window, text="Minus", font=("", 15))
self.sub_heading3.grid(column=2, sticky=tk.NS, row=1, pady=20)
def entry_add_balance(self):
addjust_balance_user = int(self.add_balance.get())
self.add_balance.delete(0, tk.END)
self.balace += addjust_balance_user
self.balance_label.configure(text=f"{self.balace}$")
#First Column
self.row_first_column += 1
new_add_label = ttk.Label(self.window, text=f"+{addjust_balance_user}$")
new_add_label.grid(column=0, row=self.row_first_column, pady=5,padx=15, sticky=tk.E)
app = App()
I have researched and tested a number of solutions on stackoverflow and other sites but I cannot get a scrollbar to work with my application.
I'm looking for a scrollbar capable of scrolling the SubFrame group that my application creates. I also need to delimit a minimum window height, and that this window can be extended, but without breaking the scrollbar.
Thank you in advance to all who will help me.
I admit not having understood which should go above the other, the Canvas or the Frame.
Here is a piece of code, with all the graphical widgets to visualize the scrollbar effect :
import tkinter as tk
from tkinter import ttk
class FrameStack(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.subframes = []
self.topFrame = tk.Frame(root)
self.topFrame.pack(side="top", fill="x")
self.groupOfFrames = tk.Frame(root)
self.groupOfFrames.pack(side="top", fill="both", expand=True, pady=(32,0))
self.scrollbar = tk.Scrollbar(self.groupOfFrames, orient="vertical")
self.scrollbar.pack(side="right",fill="y")
#self.canvas = tk.Canvas(self.groupOfFrames, yscrollcommand=self.scrollbar.set)
#self.canvas.pack()
#self.canvas_window = self.canvas.create_window(0, 0, anchor="nw", window=self.groupOfFrames)
self.create_widget()
def create_widget(self):
tk.Label(self.topFrame, text="BUS").grid(row=0, column=0)
tk.Label(self.topFrame, text="IP").grid(row=1, column=0)
tk.Label(self.topFrame, text="REG").grid(row=2, column=0)
tk.Label(self.topFrame, text="SIZE").grid(row=3, column=0)
self.combobox_bus = ttk.Combobox(self.topFrame, values=list(dic.keys()), width=40, justify='center', state="readonly")
self.combobox_bus.bind('<<ComboboxSelected>>', self.getUpdateDataIP)
self.combobox_bus.grid(row=0, column=1, sticky="nsew")
self.combobox_ip = ttk.Combobox(self.topFrame, justify='center', width=40, state="readonly")
self.combobox_ip.bind('<<ComboboxSelected>>', self.getUpdateDataReg)
self.combobox_ip.grid(row=1, column=1, sticky="nsew")
self.button_read = tk.Button(self.topFrame, text="Read", command=self.read)
self.button_write = tk.Button(self.topFrame, text="Write", command=self.write)
self.button_read.grid(row=0, column=2, columnspan=2, sticky="nsew")
self.button_write.grid(row=1, column=2, columnspan=2, sticky="nsew")
self.button_hsid = tk.Button(self.topFrame, text="HSID")
self.button_hsid.grid(row=0, column=4, columnspan=2, sticky="nsew")
self.button_add = tk.Button(self.topFrame, text="+", command=self.add_frame)
self.button_add.grid(row=1, column=4, columnspan=2, sticky="nsew")
self.combobox_reg_dl = ttk.Combobox(self.topFrame, width=40, justify='center', state="readonly")
self.combobox_reg_dl.bind('<<ComboboxSelected>>', self.getUpdateDisplayReg)
self.combobox_reg_dl.grid(row=2, column=1, sticky="nsew")
self.button_dump = tk.Button(self.topFrame, text="Dump", command=self.dump)
self.button_load = tk.Button(self.topFrame, text="Load", command=self.load)
self.button_dump.grid(row=2, column=2, columnspan=2, sticky="nsew")
self.button_load.grid(row=2, column=4, columnspan=2, sticky="nsew")
self.select_size = tk.StringVar()
self.select_size.set("0")
self.entry_size = tk.Entry(self.topFrame, textvariable=self.select_size, justify='center')
self.entry_size.grid(row=3, column=1, sticky="nsew")
tk.Scale(self.topFrame, from_=0, to=1024, variable=self.select_size)
self.select_read_size = tk.IntVar()
self.select_read_size.set(8)
self.radio_8 = tk.Radiobutton(self.topFrame, text=" 8", variable=self.select_read_size, value=8)
self.radio_16 = tk.Radiobutton(self.topFrame, text="16", variable=self.select_read_size, value=16)
self.radio_32 = tk.Radiobutton(self.topFrame, text="32", variable=self.select_read_size, value=32)
self.radio_64 = tk.Radiobutton(self.topFrame, text="64", variable=self.select_read_size, value=64)
self.radio_8.grid(row=3, column=2)
self.radio_16.grid(row=3, column=3)
self.radio_32.grid(row=3, column=4)
self.radio_64.grid(row=3, column=5)
def getUpdateDataIP(self, event):
self.combobox_ip['values'] = list(dic[self.combobox_bus.get()].keys())
self.combobox_ip.set('')
self.combobox_reg_dl['values'] = ['']
self.combobox_reg_dl.set('')
for f in self.subframes:
f.combobox_reg_rw['values'] = ['']
f.combobox_reg_rw.set('')
def getUpdateDataReg(self, event):
self.combobox_reg_dl['values'] = list(dic[self.combobox_bus.get()][self.combobox_ip.get()].keys())
self.combobox_reg_dl.set('')
for f in self.subframes:
f.combobox_reg_rw['values'] = list(dic[self.combobox_bus.get()][self.combobox_ip.get()].keys())
f.combobox_reg_rw.set('')
def getUpdateDisplayReg(self, event):
self.combobox_reg_dl.set(self.combobox_reg_dl.get()+" ("+dic[self.combobox_bus.get()][self.combobox_ip.get()][self.combobox_reg_dl.get()]+")")
def delete_frame(self, frame):
self.subframes.remove(frame)
frame.destroy()
def add_frame(self):
f = SubFrame(parent=self.groupOfFrames, controller=self)
if self.combobox_bus.get()!="" and self.combobox_ip.get()!="":
f.combobox_reg_rw['values'] = list(dic[self.combobox_bus.get()][self.combobox_ip.get()].keys())
self.subframes.append(f)
f.pack(side="top", fill="x", pady=(0,5))
def read(self):
pass
def write(self):
pass
def dump(self):
pass
def load(self):
pass
class SubFrame(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widget()
def create_widget(self):
self.combobox_reg_rw = ttk.Combobox(self, width=47, justify='center', state="readonly")
self.combobox_reg_rw.bind('<<ComboboxSelected>>', self.getUpdateDisplayReg)
self.select_val = tk.StringVar()
self.entry_value = tk.Entry(self, bd=1.5, width=20, textvariable=self.select_val, justify='center')
self.button_write = tk.Button(self, text="Write", command=self.write)
self.button_read = tk.Button(self, text="Read", command=self.read)
self.button_help = tk.Button(self, text="?", command=self.help)
self.button_remove = tk.Button(self, text="-", command=self.remove)
self.combobox_reg_rw.grid(row=0, column=0, columnspan=2, sticky="ew")
self.entry_value.grid(row=0, column=2, columnspan=2, sticky="ew")
self.button_write.grid(row=1, column=0, sticky="ew")
self.button_read.grid(row=1, column=1, sticky="ew")
self.button_help.grid(row=1, column=2, sticky="nsew")
self.button_remove.grid(row=1, column=3, sticky="nsew")
def remove(self):
self.controller.delete_frame(self)
def getUpdateDisplayReg(self, event):
self.combobox_reg_rw.set(self.combobox_reg_rw.get()+" ("+dic[self.controller.combobox_bus.get()][self.controller.combobox_ip.get()][self.combobox_reg_rw.get()]+")")
def write(self):
print(self.entry_value.get())
def read(self):
print(self.combobox_reg_rw.get())
def help(self):
pass
if __name__ == "__main__":
#dic = extractor.main()
dic = {'1': {'1.1': {'1.1.1': 'A', '1.1.2': 'B'}, '1.2': {'1.2.1': 'C', '1.2.2': 'D'}}, '2': {'2.1': {'2.1.1': 'E', '2.2.2': 'F'}, '2.2': {'2.2.1': 'G', '2.2.2': 'H'}}}
root = tk.Tk()
root.title("XXXXXX")
root.resizable(False, True)
fs = FrameStack(root)
fs.pack(fill="both", expand=True)
root.mainloop()
It is the canvas that has the ability to scroll, so your self.groupOfFrames needs to go inside the canvas. And since you want the scrollbar and canvas to appear as a single complex object, they should go in a frame.
You need to make sure that when the window is resized, the frame inside the canvas is resized as well. And you also need to make sure that when you add something to self.groupOfFrames you also update the scrollregion. These can be done by binding to the <Configure> event of each widget.
Thus, I would create the FrameStack like the following. Please note that self.topFrame and self.bottomFrame are children of self rather than root. That is a mistake I didn't catch in the code I gave you in your previous question.
class FrameStack(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.subframes = []
self.topFrame = tk.Frame(self)
self.bottomFrame = tk.Frame(self)
self.topFrame.pack(side="top", fill="x")
self.bottomFrame.pack(side="bottom", fill="both", expand=True)
self.canvas = tk.Canvas(self.bottomFrame, bd=0)
vsb = tk.Scrollbar(self.bottomFrame, command=self.canvas.yview)
self.canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.groupOfFrames = tk.Frame(self.canvas)
self.canvas.create_window(0, 0, anchor="nw", window=self.groupOfFrames, tags=("inner",))
self.canvas.bind("<Configure>", self._resize_inner_frame)
self.groupOfFrames.bind("<Configure>", self._reset_scrollregion)
def _reset_scrollregion(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def _resize_inner_frame(self, event):
self.canvas.itemconfig("inner", width=event.width)
I am doing this small project. I have a frame (initially expanded to eat up space not taken by other frames) that I added a scrollbar because this frame contains a set of widgets that replicates once a button is hit.
My problem now is that once I already added the scrollbar and make it worked, I can no longer resize the frame made from create_window option. The widgets inside it have already used the options fill and expand but still would not expand. I even specified the widths and heights in the containing widgets but still wont work.
Here are the relevant codes:
from tkinter import *
from tkinter import ttk
class Database(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
parent.state('zoomed')
self.parent = parent
#Main Frame
MainFrame=Frame(self, bg="white")
MainFrame.pack(expand = True, side = TOP, fill="both")
#Level 2 Frame
SubmainFrame=Frame(MainFrame)
SubmainFrame.pack(side=LEFT, fill = BOTH, expand = True)
#Level 3 Frame
DataFrame=Frame(SubmainFrame)
DataFrame.pack(side=RIGHT, expand = True, fill = BOTH)
frames = []
def New():
global widgetNames
global frameNames
DataFrameAF_=LabelFrame(frame,bg = "sky blue", text="Sub Info")
DataFrameAF_.pack(side = TOP, padx = 10, pady = 10, fill = BOTH, expand = True)
DataFrameAc = Frame(DataFrameAF_)
DataFrameAc.pack(side = LEFT, padx = 5)
DataFrameAc1 = Frame(DataFrameAc)
DataFrameAc1.pack(side = LEFT)
DataFrameAc2 = Frame(DataFrameAc)
DataFrameAc2.pack(side = LEFT)
#first column
self.lbl_ac2=Label(DataFrameAc1, text = "A1 ")
self.lbl_ac2.grid(row=1,column=0,sticky=W)
self.txt_ac2=ttk.Entry(DataFrameAc1, width=20)
self.txt_ac2.grid(row=1,column=1)
self.lbl_ac3=Label(DataFrameAc1, text = "A2 ")
self.lbl_ac3.grid(row=2,column=0,sticky=W)
self.txt_ac3=ttk.Entry(DataFrameAc1, width=20)
self.txt_ac3.grid(row=2,column=1)
#second column
self.lbl_ac2_=Label(DataFrameAc2, text = "B1 ")
self.lbl_ac2_.grid(row=1,column=0,sticky=W)
self.txt_ac2_=ttk.Entry(DataFrameAc2, width=20)
self.txt_ac2_.grid(row=1,column=1)
self.lbl_ac3_=Label(DataFrameAc2, text = "B2 ")
self.lbl_ac3_.grid(row=2,column=0,sticky=W)
self.txt_ac3_=ttk.Entry(DataFrameAc2, width=20)
self.txt_ac3_.grid(row=2,column=1)
DataFrameAF=LabelFrame(DataFrame, text="Information")
DataFrameAF.pack(side = TOP, pady = 10)
canvas = Canvas(DataFrameAF, borderwidth=0, background="#ffffff")
frame = Frame(canvas, background="#ffffff")
vsb = Scrollbar(DataFrameAF, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill=BOTH, expand=True)
canvas.create_window((20,20), window=frame, anchor="nw")
def reset_scrollregion(event):
canvas.configure(scrollregion=canvas.bbox("all"))
frame.bind("<Configure>", reset_scrollregion)
First_Info = New()
createWidgetButton = Button(frame, text="New Entry", command=New)
createWidgetButton.pack(side=BOTTOM, fill="x")
root = Tk()
database_window = Database(root)
database_window.pack(side="top", fill="both", expand=True)
database_window.update()
root.mainloop()
This is a part of code from my school project.
from tkinter import *
from tkinter.font import Font
class student_window():
def __init__(self, master):
self.student_win = master
#window = Toplevel(self.master)
self.student_win.geometry("1280x720")
self.header1Font = Font(family='Helvetica', size=20)
self.optionFont = Font(family='Sans Serrif', size=20)
self.student_win.focus()
self.show_window()
def show_window(self):
print("ookk")
self.student_win.title("Student Window")
self.option_frame = Frame(self.student_win, width=200, height=720)
lbl_header = Label(self.option_frame,text="EXAMINATION", font=self.header1Font, fg='white', bg='#172D44').grid(row=0,column=0, sticky=NSEW)
lbl_welcome = Label(self.option_frame, text="Welcome,", fg='#E9F1F7', bg='#2A3F54').grid(row=1,column=0)
lbl_username = Label(self.option_frame, text="Username", fg='white', bg='#2A3F54').grid(row=2,column=0)
lbl_header2 = Label(self.option_frame, text="STUDENT CORNER", fg='white', bg='#2A3F54').grid(row=3, column=0)
self.btn_tests = Button(self.option_frame, text="Attempt Exam", fg='#E9F1F7', bg='#35495D', relief=FLAT)
self.btn_tests.grid(row=4,column=0, sticky=NSEW)
self.btn_attempts = Button(self.option_frame, text="Attempts", fg='#E9F1F7', bg='#2A3F54', relief=FLAT)
self.btn_attempts.grid(row=5, column=0, sticky=NSEW)
self.btn_result = Button(self.option_frame, text="Result", fg='#E9F1F7', bg='#2A3F54', relief=FLAT)
self.btn_result.grid(row=6, column=0, sticky=NSEW)
self.btn_goBack = Button(self.option_frame, text="Go Back", fg='#E9F1F7', bg='#2A3F54', relief=FLAT)
self.btn_goBack.grid(row=7, column=0, sticky=NSEW)
self.option_frame.configure(bg='#2A3F54')
self.option_frame.grid(row=0, column=0)
self.option_frame.grid_propagate(0)
self.main_frame = Frame(self.student_win, width=880, height=720)
self.main_result_frame = Frame(self.main_frame)
self.main_result_frame.grid(row=0,column=0)
self.attempts_frame = Frame(self.main_frame)
self.attempts_frame.grid(row=0, column=0)
self.test_frame = Frame(self.main_frame)
lbl_test = Label(self.test_frame, text="In test frame").pack()
self.test_frame.grid(row=0,column=0)
self.main_frame.grid(row=0,column=1)
self.main_frame.grid_propagate(0)
self.info_frame = Frame(self.student_win, width=200, height=720)
self.btn_username = Button(self.info_frame, text="Username", relief=FLAT)
self.btn_username.grid(row=0,column=0)
self.userInfo_frame = Frame(self.info_frame)
self.info_frame.grid(row=0, column=2)
self.info_frame.grid_propagate(0)
root = Tk()
student_window(root)
root.mainloop()
And it looks something like this.
The Student Panel for my project
The whole window is divided into three frames and want to expand each label and button of the left frame(self.option_frame) to fill it horizontally. I tried doing sticky=EW and sticky=NSEW but still some space is left. How do I fix that?
You need to call self.option_frame.columnconfigure(0, weight=1) to make column 0 to use all the available horizontal space.
I was just trying some things and what I have found to be working is to make the label width bigger than than the frame then anchoring the text to the left.
The problem I'm currently stuck with is that when I add a new shift I can't get the GUI to update properly. Currently the best I can seem to get is getting the updated information but it is displayed on top of the old information.
I did read about the whole mainloop() issue and tried to get use the after() method but I'm not sure if thats what I'm looking for:
class ViewShifts(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
buttonFrame = tk.Frame(self)
buttonFrame.pack(side='bottom', fill='both', expand=True)
#frame to display shifts
table = tk.Frame(self)
table.pack(side='top', fill='both', expand=True)
table.destroy()
#dictionary which will contain widgets that will display shifts (in viewNew method)
self.widgets = {}
button1= tk.Button(self, text="Home",
command=lambda: controller.showFrame(StartPage))
button1.pack(padx=0, pady=10, side='bottom')
button2 = tk.Button(self, text='Update',
command=lambda: deleteShiftFrame(self))
button2.pack(padx=0, pady=10, side='bottom')
text = tk.Label(self, text="All shifts", font=LARGEFONT)
text.pack(padx=10, side='top')
def deleteShiftFrame(self):
table.destroy()
viewNew(self)
def viewNew(self):
#frame to display shifts
table = tk.Frame(self)
table.pack(side='top', fill='both', expand=True)
row = 0
#make dictionary empty
self.widgets = {}
c.execute("SELECT * FROM shifts")
data = c.fetchall()
#create labels for each column
date_label = tk.Label(table, text="Date")
shift_label=tk.Label(table, text="Shift")
shop_label=tk.Label(table, text="Shop")
hours_label=tk.Label(table, text="Hours")
#add labels to grid
date_label.grid(row=0, column=0, sticky="nsw")
shift_label.grid(row=0, column=1, sticky="nsw")
shop_label.grid(row=0, column=2, sticky="nsw")
hours_label.grid(row=0, column=3, sticky="nsw")
#for each column create a tk label for each row within column with corresponding details
for id, date, shift, shop, hours in (data):
row+=1
self.widgets[id] = {
"id":tk.Label(table, text=id),
"date":tk.Label(table, text=date),
"shift":tk.Label(table, text=shift),
"shop":tk.Label(table, text=shop),
"hours":tk.Label(table, text=hours)
}
#add current row of column to grid
self.widgets[id]["date"].grid(row=row, column=0, sticky="nsw")
self.widgets[id]["shift"].grid(row=row, column=1, sticky="nsw")
self.widgets[id]["shop"].grid(row=row, column=2, sticky="nsw")
self.widgets[id]["hours"].grid(row=row, column=3, sticky="nsw")
#add blank column between each field for spacing
table.grid_columnconfigure(0, weight=1)
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
table.grid_columnconfigure(3, weight=1)
#add extra row for padding
table.grid_rowconfigure(row+1, weight=1)
viewNew(self)
class AddShifts(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
text = tk.Label(self, text = "Add shifts", font=LARGEFONT)
text.pack(padx=10, side = 'top')
#create frame for entry
AddShiftFrame = tk.Frame(self)
AddShiftFrame.pack(side="top", fill="both", expand=True)
#add column headers
dateLabel = tk.Label(AddShiftFrame, text="Date")
shiftLabel = tk.Label(AddShiftFrame, text="Shift")
shopLabel = tk.Label(AddShiftFrame, text='Shop')
dateLabel.grid(row=1, column=0, sticky="nsw")
shiftLabel.grid(row=1, column=1, sticky="nsw")
shopLabel.grid(row=1, column=2, sticky="nsw")
#create dictionary of widgets
self.widgets = {
"Date":tk.Entry(AddShiftFrame),
"Shift":tk.Entry(AddShiftFrame),
"Shop":tk.Entry(AddShiftFrame)
}
#add widgets to frame
self.widgets["Date"].grid(row=2, column=0, sticky="nsw")
self.widgets["Shift"].grid(row=2, column=1, sticky="nsw")
self.widgets["Shop"].grid(row=2, column=2, sticky="nsw")
#this method will submit the data (callback function)
def submit_data(self):
shift_data = [(self.widgets["Date"].get()), (self.widgets["Shift"].get()), (self.widgets["Shop"].get())]
c.execute("INSERT INTO shifts (date, shift, shop) VALUES(?,?,?)", (shift_data[0], shift_data[1], shift_data[2]))
conn.commit()
print("done")
#submit button
submit = tk.Button(self, text="Submit",
command= lambda: submit_data(self))
submit.pack(padx=10, pady=10, side="left")
#home button
button1 = tk.Button(self, text="Home",
command=lambda: controller.showFrame(StartPage))
button1.pack(padx=10, pady=10, side='left')
app = MainApplication()
app.after(1000, getShifts)
app.mainloop()