I am creating a program with two frames in one window. The first has input fields, the second will create a graph.
I found a way to create input fields dynamically from a list and get their values accordingly, but I can't get them to show on the window. When I run the program it shows an empty window.
What should I do to get the label and input widgets to show on the first frame (InputPage)? I tried changing 'parent' to 'self' but it made no difference. I don't really understand the structure of widgets in multiple frame applications.
Here is my code:
from tkinter import *
namesInput = ["first", "second", "third", "fourth", "fifth"]
entryInput = {}
labelInput = {}
root = Tk()
class ZorgplanGrafiek(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (InputPage, GraphPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(InputPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class InputPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self,parent)
label = Label(self, text="Zorgplan input")
label.pack(pady=10,padx=10)
i = 0
for name in namesInput:
e = Entry(self)
entryInput[name] = e
lb = Label(self, text=name)
labelInput[name] = lb
i += 1
#def print_all_entries():
# for name in namesInput:
# print( entryInput[name].get())
class GraphPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="The graph will show here")
label.pack(pady=10,padx=10)
button = Button(self, text="Back to Home",
command=lambda: controller.show_frame(InputPage))
button.pack()
app = ZorgplanGrafiek()
app.mainloop()
Firstly delete root = Tk() at the top of your code you are creating 2 windows. Secondly, your loop to create the entry and label widgets is not correct therefore they are not displayed on the frame, so that is your answer for why they wont show.
Try this:
class InputPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self,parent)
label = Label(self, text="Zorgplan input")
label.grid(row=0, column=0, sticky ='n', columnspan =2)
# i brought your variable in the class for example sake
namesInput = ["First:", "second:", "Third:", "Fourth:", "Fifth:"]
self.entryWidgets = [] # we want to call this in another function so we assign it as self.variableName
labelWidgets = []
#LOOP TO CREATE WIDGETS
for i in range(0, len(namesInput)):
labelWidgets.append(Label(self, text = namesInput[i]))
self.entryWidgets.append(Entry(self))
labelWidgets[-1].grid(row= i+1, column =0, sticky='e')
self.entryWidgets[-1].grid(row= i+1, column = 1, sticky='w')
submit = Button(self, text = "Submit", command = self.getEntries)
submit.grid(row = 6, column =0, columnspan =2)
def getEntries(self):
results = []
for x in self.entryWidgets: # i.e for each widget in entryWidget list
results.append(x.get())
print(results)
Code explanation:
We are iteratively creating widgets to the number of elements within namesInput list. Each time we create a widget we add it to their respective list. E.g for entry widgets we created a list called entryWidgets. We append them to a list so that we can reference them individually later on when we want to do something with them.
Furthermore, i changed pack() to grid(). The grid method is much cleaner and gives us more control over the layout of our window in my opinion.
Note - If you're struggling to understand how i 'grided' the widgets in the way i did, i just drew up a quick sketch of the widgets with co-ordinates representing their row and column and then from there its fairly easy to see how to manipulate the grid settings in the for loop.
Screenshot:
Related
I'm having an update problem in tkinter GUI, for past two days, I have searched a lot, Cant find something specific to my problem.This post Tkinter updating labels in stacked frame windows come close to my problem but not exactly. I am using classes to structure my application... The structure is given here Application structure image ( SOF not letting me embed images but link is provided )
From above structure you can see, I'm trying to make changes in DetailFrame from ListProduct Frame, now the code is reaching there and changing the values successfully but not updating the label, I'm using config method to change label... and frame background,but no luck..
I have tried StringVar as well for updating label, but nothing... Sample Code is provided below...
This application is a part of main app and for Original Code Structure Thanks to .. Bryan Oakley
class ProductWindow(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("600x500")
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.notebook = ttk.Notebook(container)
self.notebook.grid(row=0, column=0, sticky='nsew')
self.new_product_tab = tk.Frame(self.notebook, bg='#233223' )
self.list_product_tab = tk.Frame(self.notebook, bg='#323232')
self.edit_product_tab = tk.Frame(self.notebook, bg='#433434')
# Adding Tabs to Notebook
self.notebook.add(self.new_product_tab, text=" Add New Product ")
self.notebook.add(self.list_product_tab, text=" List All Product ")
self.notebook.add(self.edit_product_tab, text=" Edit Product ")
self.productframe = EditProductFrame(self.edit_product_tab)
self.detailframe = DetailFrame(self.productframe)
button = tk.Button(self.list_product_tab, text="Change background in Edit Form", command=self.change_method)
button.pack()
def change_method(self):
print("Trying to change the frame")
self.productframe.raise_edit_frame(DetailFrame)
self.detailframe.change_bg('green')
self.notebook.select(self.edit_product_tab)
if __name__ == "__main__":
testObj = ProductWindow()
testObj.mainloop()
In another file, I have DetailFrame below.
class EditProductFrame(tk.Frame):
def __init__(self, parent):
print("Edit product frame constructor is called...")
tk.Frame.__init__(self, parent)
self.pack(side='top', fill='both', expand=True)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# define frames and pack them in
self.frames = {}
for F in {DetailFrame, EditFrame}:
frame = F(self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.raise_edit_frame(DetailFrame)
def raise_edit_frame(self, container):
frame = self.frames[container]
frame.tkraise()
class EditFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.config(bg='green')
label = tk.Label(self, text="Edit Page",)
label.pack(pady=0,padx=0)
tk.Button(self, text="Go to Detail", command=lambda:parent.raise_edit_frame(DetailFrame)).pack()
class DetailFrame(tk.Frame):
def __init__(self, parent):
print("something detail view")
tk.Frame.__init__(self, parent)
self.config(bg='blue')
self.label = tk.Label(self, text='Original Label')
self.label.pack(pady=0,padx=0)
tk.Button(self, text="Go to Edit Frame", command=lambda:parent.raise_edit_frame(EditFrame)).pack()
def change_bg(self, color):
# doesn't update the background
self.config(bg=color)
# doesn't update the Label text
self.label.config(text='Changed Label')
# print the correct changed value = 'Changed Label'
print(self.label.cget('text'))
Thanks ...
Note that you have created another instance of DetailFrame (self.detailframe) inside ProductWindow but it is not visible since no layout function is called on it. Actually there is already an instance of DetailFrame created when creating instance of EditProductFrame, so you need to call change_bg() on this instance instead:
class ProductWindow(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("600x500")
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.notebook = ttk.Notebook(container)
self.notebook.grid(row=0, column=0, sticky='nsew')
self.new_product_tab = tk.Frame(self.notebook, bg='#233223' )
self.list_product_tab = tk.Frame(self.notebook, bg='#323232')
self.edit_product_tab = tk.Frame(self.notebook, bg='#433434')
# Adding Tabs to Notebook
self.notebook.add(self.new_product_tab, text=" Add New Product ")
self.notebook.add(self.list_product_tab, text=" List All Product ")
self.notebook.add(self.edit_product_tab, text=" Edit Product ")
# -- there is an instance of DetailFrame created inside EditProductFrame
self.productframe = EditProductFrame(self.edit_product_tab)
# -- so don't create another instance of DetailFrame
#self.detailframe = DetailFrame(self.productframe)
button = tk.Button(self.list_product_tab, text="Change background in Edit Form", command=self.change_method)
button.pack()
def change_method(self):
print("Trying to change the frame")
self.productframe.raise_edit_frame(DetailFrame)
#self.detailframe.change_bg('green')
# -- call change_bg() on the instance of DetailFrame inside EditProductFrame
self.productframe.frames[DetailFrame].change_bg('green')
self.notebook.select(self.edit_product_tab)
Another option is to make self.detailframe the reference to the instance of EditFrame inside EditProductFrame:
self.detailframe = self.productframe.frames[DetailFrame]
I want the buttons to be on opposite sides of the screen, but I'm unsure as to why they keep positioning themselves in the middle. The code is set up like this because I plan on having multiple overlays that I switch between with the show_frame method. The class InputWindow is one of those overlays. When I created a basic script with only window and buttons, I was able to get the buttons positioned properly, but I'm unsure as to what I'm doing incorrectly here that's preventing me from distancing the buttons properly.
import tkinter as tk
class GuiController(tk.Tk):
def __init__(self, *args , **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
frame = InputWindow(container, self)
self.frames[InputWindow] = frame
frame.pack()
self.show_frame(InputWindow)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
controller.geometry("650x500")
button_defaultGame = tk.Button(self, text = "Default Game")
button_defaultGame.grid(row = 0, column = 0, sticky = "W")
button_test = tk.Button(self, text = "Test")
button_test.grid(row = 0, column = 1, sticky = "E")
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=2)
app = GuiController()
app.mainloop()
When struggling with layout problems it helps to give your frames distinctive colors. Otherwise it can be difficult to see where one frame ends and another begins.
For example, give the container a color like this:
container = tk.Frame(self, bg="bisque")
Next, give your InputWindow a different color like this:
class InputWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg="pink")
When you run the code you'll see something like this:
This immediately makes it clear that InputWindow is not filling the container window. Looking through your code we can see that you're doing frame.pack() to add the instance of InputWindow to container.
Instead, you need to request that InputWindow fills the container window. You do that by changing your call to pack to look like this:
frame.pack(side="top", fill="both", expand=True)
Now we can see that the instance of InputWindow fills the container, and your buttons do indeed sit on the edges of the window.
In tkinter I have step up multiple pages, each page should have a unique Listbox that will be populated with unique information.
My problem is the 'listbox' shows the information from my initial page on the other pages. Even if I completely remove the List box from the other pages, the 'Listbox` from my first page still shows up.
This is basically the first time I have used classes with anything, so I am not sure why this isn't working. This is all basically copied form YouTube how to's, and I am trying to bend it to what I need.
class Uploader(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs) #sets up tk window stuff
container = tk.Frame(self)
container.grid(row = 0, column = 0)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Gr7, Gr8, Gr9): #put new pages on this
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column = 0, sticky = "nsew")
self.show_frame(Gr7)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Gr7(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self, parent)
lb = tk.Listbox(width=30, height=15)
lb.insert('end', *homelist)
lb.grid(row = 1, column = 0)
class Gr8(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self, parent)
lb1 = tk.Listbox(width=30, height=15)
lb1.insert('end', *homelist1)
lb1.grid(row = 1, column = 0)
class Gr9(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self, parent)
lb2 = tk.Listbox(width=30, height=15)
lb2.insert('end', *homelist2)
lb2.grid(row = 1, column = 0)
You are not specifying which widget should contain the listbox, so all of your listboxes are given the root window as its master. Because you are putting them all in the same row and column, you only see one listbox.
To fix this -- and as a good general rule of thumb -- you should always explicitly provide the master when creating widgets:
lb = tk.Listbox(self, width=30, height=15)
I am trying to develop a Tkinter GUI which contains two pages, the first one to input stock name (counter_selection) and the second one to plot stock price data. However, when I tried to use the data input from the first class using controller.get_page function it does not return the value input by Entry. The code is as below:
class Application(Tk.Tk):
'''A GUI Application for FCN Demo'''
def __init__(self):
'''Initialization of frame'''
Tk.Tk.__init__(self)
container = Tk.Frame(self)
container.pack(side='top', fill = 'both', expand = True)
container.grid_rowconfigure(0,weight = 1)
container.grid_columnconfigure(0,weight = 1)
self.frames = {}
for F in (StartPage, counter_selection, plot_counter):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row= 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def get_page(self,classname):
'''Returns an instace of page given it's class name as a string'''
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class counter_selection(Tk.Frame):
def __init__(self, parent, controller):
'''This is to create the widgets to input stock'''
Tk.Frame.__init__(self,parent)
self.controller = controller
label1 = Tk.Label(self, text = "Please Enter 3 counter names")
label1.pack(padx = 10, pady = 10)
self.entry1 = Tk.Entry(self)
self.entry1.pack()
self.entry2 = Tk.Entry(self)
self.entry2.pack()
self.entry3 = Tk.Entry(self)
self.entry3.pack()
button1 = Tk.Button (self, text = 'Confirm', command = lambda: controller.show_frame(plot_counter))
button1.pack()
class plot_counter(Tk.Frame):
'''This is to plot the graph of three selected counters'''
def __init__(self,parent,controller):
Tk.Frame.__init__(self,parent)
self.controller = controller
counterPage = self.controller.get_page('counter_selection')
self.counter1 = counterPage.entry1.get()
self.counter2 = counterPage.entry2.get()
self.counter3 = counterPage.entry3.get()
label1 = Tk.Label(self, text = self.counter11)
label1.pack()
The label1 does not show anything on the Frame, suggesting that it seems failed to get the value from the class. What's my mistake?
(PS: I didn't put the StartPage in since it is irrelevant for the question)
Why is Tkinter Entry's get function returning nothing?
TL;DR: your call to entry1.get() is done only once while instancing the plot_counter object, and never called again. Try putting it in a function to be called when displaying the Frame.
The two frames in my program, ViewSubjects and AddSubjects both contain frames and widgets that are not stretching to fill the screen. Why are they doing this and how would I go about fixing it?
ViewSubjects Frame
AddSubjects Frame
Here is my code:
import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as tks
class Program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default = "")
tk.Tk.wm_title(self, "")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (SubjectHome, ViewSubject, AddSubject):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(SubjectHome)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class SubjectHome(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")
name = tk.Label(self, text = "User: FirstName + LastName")
name.pack(anchor="ne")
pagename = tk.Label(self, text = "Subject Menu")
pagename.pack(anchor="n")
self.innerFrame = tk.Frame(self, bg="red")
self.innerFrame.place(relx=.5, rely=.5, anchor="c")
view = ttk.Button(self.innerFrame, text = "View Subjects", command = lambda: controller.show_frame(ViewSubject))
view.grid(row=0, sticky="W"+"E")
add = ttk.Button(self.innerFrame, text = "Add Subjects", command = lambda: controller.show_frame(AddSubject))
add.grid(row=1, sticky="W"+"E")
class ViewSubject(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
innerFrame = tk.Frame(self)
innerFrame.place(relx=.5, rely=.5, anchor="c")
firstFrame = tk.Frame(innerFrame)
firstFrame.grid(row=0, sticky="WE")
secondFrame = tk.Frame(innerFrame)
secondFrame.grid(row=1, sticky="WE")
self.text = tks.ScrolledText(firstFrame)
self.text.grid(rowspan=3, columnspan=3 ,sticky="E")
scrollbar = tk.Scrollbar(secondFrame, orient="vertical")
lb = tk.Listbox(secondFrame, yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
scrollbar.pack(side="right", fill="y")
lb.pack(side="left",fill="both", expand=True)
for x in range(15):
lb.insert("end", x)
back = ttk.Button(innerFrame, text = "Back", command = lambda: controller.show_frame(SubjectHome))
back.grid(row=2, sticky="W")
next = ttk.Button(innerFrame, text = "Next")
next.grid(row=2, sticky="E")
class AddSubject(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
name = tk.Label(self, text = "User: FirstName + LastName")
name.pack(anchor="ne")
pagename = tk.Label(self, text = "Add Subjects")
pagename.pack(anchor="n")
self.innerFrame = tk.Frame(self)
self.innerFrame.place(relx=.5, rely=.5, anchor="c")
canvas = tk.Canvas(self.innerFrame)
self.firstFrame = tk.Frame(canvas)
self.firstFrame.pack(anchor="n")
info = tk.Label(self.innerFrame, text = "Information...\n Information....")
info.pack()
for x in range(5):
pagename = tk.Label(self.firstFrame, text = "Unit Name")
pagename.grid(row=0, column=x)
self.text = tks.ScrolledText(self.firstFrame, width=50)
self.text.grid(row=1, column=x ,sticky="E")
scrollbar = tk.Scrollbar(self.innerFrame, orient="horizontal", command=canvas.xview)
canvas.configure(xscrollcommand=scrollbar.set)
scrollbar.pack(side="bottom", fill="x")
canvas.pack(side="left", fill="both", expand=True)
back = ttk.Button(self.innerFrame, text = "Back", command = lambda: controller.show_frame(SubjectHome))
back.pack(anchor="sw")
next = ttk.Button(self.innerFrame, text = "Next")
next.pack(anchor="se")
app = Program()
app.state('zoomed')
app.mainloop()
Most of your problems boil down to three common mistakes:
First, you are using place, and place by default doesn't make windows grow or shrink. It assumes you created widgets to be the exact size you want them to be. You need to explicitly tell tkinter how you want it to handle extra space.
In your case you probably want to set the relative width and relative height of the inner frames to 1.0 (ie: 100% of the width and height of the containing window) with the relwidth and relheight options. This will force the inner frame to always be exactly as tall and wide as its parent.
self.innerFrame.place(..., relwidth=1.0, relheight=1.0)
Second, you are using grid within the inner frames, but you are failing to give any rows or columns a weight. The weight tells tkinter how to allocate extra space. By default, extra space goes unused. Since you want your widgets to fill the extra space, you need to give at least one row and one column a positive weight.
Third, you're trying to solve too many problems at once. My advice is to remove all of the widgets except one frame. Get it to fill, grow and shrink the way you want. Then, add its immediate children and do likewise. Then, add any grandchildren, and continue on, one group of widgets at a time, until you're satisfied with that frame. Only then should you move on and fix another frame.
protip: give each frame a unique color during development. This makes it easy to see which frames are growing or shrinking, and which ones are not.