I have a tkinter project, the main script (not a full program, to long) looks like this, label (led_A) is created inside a TK class,
....
def fun_A:
...
#need to hidden led_A
#**need to implement** led_A.forget() or .place()
class home(tk.Tk)
def __init__(self, *args, **kwargs)
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Graph ")
self.geometry("1000x800")
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 = {}
F=Graph
frame=Graph(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Graph)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Graph(tk.frame):
def __init__(self,parent,controller):
...
photo = PhotoImage(file='1.jpg')
led_A = label(self, image = photo)
app = home()
app.mainloop()
So I try to implement led_A.forget in fun_A, how could I do? I tried global led_A but main program could not recognize it. Thanks
To manipulate an object you need a reference to the object. In this specific case, you need to make led_A an instance attribute so that you can reference it. You also need to add a method in your controller to give you access to the instance of Graph.
To make led_A an instance attribute:
class Graph(tk.frame):
def __init__(self,parent,controller):
...
photo = PhotoImage(file='1.jpg')
self.led_A = label(self, image = photo)
...
To add a method to the controller, do something like this:
class home(tk.Tk)
...
def get_frame(self, frame_class):
return self.frames[frame_class]
Ideally, func_A shouldn't know anything about how Graph is implemented since func_A isn't inside Graph. That means that you need to add a function to Graph which will add or remove the led.
class Graph(tk.frame):
...
def turn_on_led(self):
self.led_A.grid(...)
Finally, you need to modify fun_A to use this method to get a reference to the graph and call the function:
def fun_A():
graph = app.get_frame(Graph)
graph.turn_on_led()
Related
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.
Note I tried to put this in stack overflow with correct formatting, but even when I pressed the code formatting button, it didn't process the response. Can someone help me with that as well?. I am posting here without the formatting as I need help quite fast. I know many people on this forum are really good at programming, so I thought of reaching out.
I am working on an application using python, and more specifically, I'm using Tkinter for the user interface. I have over twenty new pages I need to make within the application, and I as a result, I thought of using a for loop and an outline class structure of which I would create new instances (as their own pages which would later be linked around the application using buttons) to be the classes I use. However, I continue to get the following error when I run my code:
File "setup.py", line 215, in <module>
pages_dict[info_req[info][filetype][0]] = outline_info(new_object)
TypeError: __init__() takes exactly 4 arguments (2 given)
I understand why this makes sense, as the init function definition contains 4 arguments, but I am not sure about how I can make the controller (which is defined in the initial application class) and the parent window part of the arguments for the instance of the outline_info class, as I cannot refer to self in those scenarios as the classes haven't even been declared or made up until that point of declaration as an instance (If this explanation seems confusing, please look at the code below for further clarification as well).
An excerpt of my code is shown below, addressing the above concerns. Please let me know if more information is needed to understand or clarify my problem. The code below does not include many other classes I have defined, as well as an array called info_req, which contains a database of information.
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 (PageOne, PageThree, PageFour, indpage, familypage, FinalPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("PageOne")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class outline_info(tk.Frame):
def __init__(self, parent, controller, info_type_array):
count = 0 # Just for reference
tk.Frame.__init__(self, parent)
self.controller
first_title = Label(self, text=info_type_array[0]).grid(row=1,column=1)
for i in range(1, len(info_type_array)):
text_first = Label(self, text=str(info_type_array[i])).grid(row=d+1, column=1)
entry_first = Entry(self, width=20).grid(row=d+1, column=2)
count += 1
def submit_again():
controller.show_frame("indpage")
return "hello" # Do I still need this?
submit2 = Button(self, text="Submit Details", bg="blue", command=submit_again)
submit2.pack(side=BOTTOM)
pages_dict = {}
for i in range(0, len(info_req)):
for filetype in range(0, len(info_req[i])):
if filetype!=0:
new_object = info_req[i][filetype]
if info_req[i][filetype][0] not in pages_dict.keys():
pages_dict[info_req[i][filetype][0]] =outline_info(new_object)
Many thanks.
Edit:
The following is a snippet of info_req. I am creating a beginner's travel guide, but I really want to learn how to tackle the problem in the original post.
info_req = [ [ ["Places Visited"], ["Country 1", "City Visited", "Reccomendations"], ["Country 2", "City Visited", "Reccomendations"] ], [ ["Favorite Food"], ["Food 1", "Cuisine", "Taste", "Flavor"], ["Food 2", "Cuisine", "Taste", "Flavor"] ], [ ["Favorite Airlines"], ["Airline 1", "Organization", "Position", "Duration"] ] ]
Solution
As you want the controller to be accessible, you can make the outline_info class inherit the Application class. Then, call super().__init__() in the outline_info class and DO NOT instantiate the Application class but the outline_info class. Here is your code with that:
Code
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 (PageOne, PageThree, PageFour, indpage, familypage, FinalPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("PageOne")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class outline_info(Application):
def __init__(self, parent, controller, info_type_array):
count = 0 # Just for reference
tk.Frame.__init__(self, parent)
self.controller
first_title = Label(self, text=info_type_array[0]).grid(row=1,column=1)
for i in range(1, len(info_type_array)):
text_first = Label(self, text=str(info_type_array[i])).grid(row=d+1, column=1)
entry_first = Entry(self, width=20).grid(row=d+1, column=2)
count += 1
def submit_again():
controller.show_frame("indpage")
return "hello" # Do I still need this?
submit2 = Button(self, text="Submit Details", bg="blue", command=submit_again)
submit2.pack(side=BOTTOM)
pages_dict = {}
for i in range(0, len(info_req)):
for filetype in range(0, len(info_req[i])):
if filetype!=0:
new_object = info_req[i][filetype]
if info_req[i][filetype][0] not in pages_dict.keys():
pages_dict[info_req[i][filetype][0]]=outline_info(new_object)
There are some formatting errors which hopefully you can fix! Use the same indentation everywhere (Some places 8 space and some places 4 spaces you have used).
Suggestions and Other Information
You have used tk.wdg in some places and directly wdg in some cases. Note in the code I have posted, I haven't posted a working example but how the soultion works since the source code contains much more code than you have posted thus making the flow unclear. If you want anything to be usable in the Application class, just use it with self since self is actually an object of submit_info passed on to Application class. Hope this helped!
I'm trying to replicate an answer to my own accord from this answer/question here: Switch between two frames in tkinter
As you can see from the answer above, tehy instantiate multiple sub-classes of the Frame widget, and when we want to switch pages, we click a button and that class a method from our base class.
However, wouldn't creating multiple 'pages' methods under a 'Pages' class, be a lot cleaner and make sense? I'm not sure what to believe, and I would love clarification as to how I should be tackling this project, and why using classes or instanced methods would be better?
I've added my comments into the code below for lines I don't quite understand and I'm hoping I can gain some knowledge from the world of StackOverflow.
import Tkinter as tk
LARGE_FONT = ("Verdana", 12)
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
########### are we calling the Tk class from tkinter and passing in our 'Main' class?
tk.Tk.__init__(self, *args, **kwargs)
# why not self.container?
container = tk.Frame(self)
# again, should I be using self.page here instead?
page = Pages(parent=container, controller=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 ('page.page_one()', 'page.page_two()'):
# what exactly does __name__ do? And how can I replicate this with my derived classes instanced methods?
page_name = F#F.__name__
frame = page#(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame("page.page_one()")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class Pages(tk.Frame):
# i could just use *args, **kwargs here couldn't I?
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.page_one(controller)
# here I have my instance methods inside my derived 'Pages' class
# isn't this much cleaner, having multiple instance methods inside a class?
# or I should be separating each page into it's own instanced class?
def page_one(self, controller):
label = tk.Label(self, text='show_firmware_page', font=LARGE_FONT)
label.pack(pady=10, padx=10)
# how can I switch pages using the next button?
next_btn = tk.Button(self, text='Next', command=lambda: controller.show_frame(self.page_two(controller)))
quit_btn = tk.Button(self, text='Quit', command=lambda: controller.show_frame())
quit_btn.pack()
next_btn.pack()
def page_two(self, controller):
label = tk.Label(self, text='second_page', font=LARGE_FONT)
label.pack(pady=10, padx=10)
# how can I switch pages using the next button?
next_btn = tk.Button(self, text='Next', command=lambda: Pages.show_frame("page_one"))
quit_btn = tk.Button(self, text='Quit', command=lambda: Pages.show_frame())
quit_btn.pack()
next_btn.pack()
app = Main()
app.mainloop()
Basically, my current push is to try and use methods within my class in order to define my pages and switch between them. I'm currently having some trouble, and upon taking a look at other's answers, a lot of them have the idea that each class instantiates a Frame, in which we call a method to switch between those instances.
Let me know your thoughts on this process to get me up to speed about how I should be tackling this project.
Many thanks in advance for your help - I really want to get this OOP stuff down.
You could theoretically make methods in a single class reconfigure the UI the way you want, but it's probably not going to be easy.
Your current code can't work because there's no simple way for one of your methods to clean up the work done by another previous method (e.g. by getting rid of the label and buttons it created).
The original answer you linked to avoided that issue by having all the widgets created at the start of the program (not only when they're about to be displayed). Only one page is displayed at a time though, since they're all Frames that have been configured to display in the same location. The Frame that is on top hides the others and prevents their widgets from doing anything. By moving a different frame on top, you can switch between the pages.
If we ignore those widget display issues, you could make your main class call the methods you've written:
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
container = tk.Frame(self) # I'm not sure a separate container is necessary
container.pack(side="top", fill="both", expand=True) # since we're only putting
container.grid_rowconfigure(0, weight=1) # one other frame inside it
container.grid_columnconfigure(0, weight=1)
page = Pages(parent=container, controller=self)
page.grid(row=0, column=0, sticky='nsew') # moved up from below
self.frames = {}
for F in (page.page_one, page.page_two): # no quotes or parentheses
page_name = F.__name__ # the names are the strings "page_one" and "page_two"
self.frames[page_name] = F
self.show_frame("page_one")
def show_frame(self, page_name):
method = self.frames[page_name]
method() # call the method we are switching to
class Pages(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
def page_one(self):
# do something to clean up previous pages here?
label = tk.Label(self, text='show_firmware_page', font=LARGE_FONT)
label.pack(pady=10, padx=10)
next_btn = tk.Button(self, text='Next',
command=lambda: self.controller.show_frame("page_two")
next_btn.pack()
def page_two(self):
label = tk.Label(self, text='second_page', font=LARGE_FONT)
label.pack(pady=10, padx=10)
next_btn = tk.Button(self, text='Next',
command=lambda: self.controller.show_frame("page_one"))
next_btn.pack()
This will work (for some definition of "work"). I removed the quit buttons because I'm not sure exactly what the best way to handle them is (you probably don't want them to be calling show_frame).
Note that I'm by no means an expert at TKinter, so it's entirely possible that there's some easy way to remove the widgets from the previous page when you've moved on to the next one. I just don't know how.
I'm building a GUI with code that originally came from another stack exchange answer. I've modified it since I want to pass variables to following pages and have them display the values. As a way to do this, rather than display on a button event, I am trying to create the page with the show() method:
import Tkinter as tk
#import vpnRename12 as Rename
#import vpnRename_pullconfig as pullConfig
"""
Create multiple pages to go through in the same frame. Use lift() to bring
the desired page into view and stack them on top of one another. Define
page-specific methods in each page.
"""
class vpnRenameProgram(tk.Tk):
"""
Create a page class that will allow you to lift to front the applicable
page with the proper functions to keep the script running.
"""
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
# Create an empty dictionary to contain all of the page names
self.frames = {}
self.show("MainView")
# Instantiate the next page on call, rather than at start to allow
flexibility
def instantiate_page(self, cls):
"""
Since all of the pages are defined in the same scope/namespace, we
can use
the globals()[] dict to find and instantiate the pages dynamically
with the show() method.
cls is the class argument we are doing a lookup on in the global()
dict.
"""
try:
newframe = globals()[cls](container,self)
page_name = newframe.__name__
except:
print("\nError defining inline class %s"%cls)#("Class %s is not defined" %cls)
newframe = None
page_name=globals()[cls].__name__
return newframe, page_name
# Create lift function to bring desired page to front of view,
#instantiate page if
# it isn't already (check frames dict)
def show(self, cls):
if cls not in self.frames.keys():
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
frame, page_name = self.instantiate_page(cls)
if frame==None:
frame = globals()[cls](parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="news")
frame = self.frames[cls]
frame.lift()
def get_page(self, classname):
"""
Return instance of page when it's class name is passed in as string
"""
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
class MainView(tk.Frame):
def __init__(self,parent, controller,**kwargs):
tk.Frame.__init__(self,parent)
self.controller = controller
self.edit_directory="edit_dir"
self.complete_directory ="comp_dir"
TitleLabel = tk.Label(self, text="VPN Script Editor")
TitleLabel.pack({"side":"top"})
EditLabel = tk.Label(self, text="Edit File Directory:
%s"%self.edit_directory)
EditLabel.pack()
CompLabel = tk.Label(self, text="Completed File Directory:
%s"%self.complete_directory)
CompLabel.pack()
Next = tk.Button(self, text="Next", command=lambda:
controller.show("listVPN"))
Next.pack()
class listVPN(tk.Frame):
"""
This is the second page, it contains a text box where you will list the
names of the vpn's that you want to edit. It will also display the
directories
obtained by the pullconfig script.
"""
def read_list(self):
vpn_list=str(self.var_vpn_list.get()).upper()
return vpn_list
def __init__(self, parent, controller, **kwargs):
self.controller = controller
tk.Frame.__init__(self, parent)
self.var_vpn_list = tk.StringVar()
label=tk.Label(self, text="Please list the VPNs desired to edit")
label.pack()
#Create text box to submit a list of vpns back to the main program
vpnLabel = tk.Label(self, text="VPN Names").pack()
self.TextBox = tk.Entry(self, textvariable=self.var_vpn_list)
self.TextBox.pack()
vpnListSubmit = tk.Button(self, text="Enter", command= lambda:
self.read_list() and self.controller.show("pickFiles"))
vpnListSubmit.pack()
class pickFiles(tk.Frame):
"""
Second page that allows you to select your desired files from the
edit directory specified in the config file. Check all desired files,
list will be returned to the program.
"""
def get_vpn_list(self):
list = self.controller.get_page("listVPN").var_vpn_list.get()
self.vpn_list = str(list).upper()
self.vpn_label.configure(text="VPN List: %s"%self.vpn_list)
return self.vpn_list
def __init__(self, parent, controller,**kwargs):
# Inherits from the tk.Frame class
tk.Frame.__init__(self, parent)
self.controller = controller
self.vpn_list = tk.StringVar()
list = self.controller.get_page("listVPN").var_vpn_list.get()
self.vpn_list = str(list).upper()
show_vpn = tk.Button(self, text="Show vpnlist", command =
self.get_vpn_list)
show_vpn.pack()
self.vpn_label = tk.Label(self, text="VPN List: %s" %self.vpn_list)
self.vpn_label.pack()
# todo: get external module function to run with variable input
#file_list = Rename.searchFile(vpnlist)
# Execute program on calling the parent class
if __name__=="__main__":
app = vpnRenameProgram()
app.mainloop()
EDIT:
Above is my whole code with custom scripts I've imported commented out. My main question is about layout. I want the frames to stack on top of one another, but they are not. Why is it doing this and what would get me on track to getting the layout I want?
The main problem with your code is that you're creating multiple containers. The code that served as a base for your program was specifically designed to have a single container with multiple frames within the container.
The first step is to create the container, and save a reference so it can be used later:
class vpnRenameProgram(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand=True)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.show_frame("MainView")
I'm also going to recommend that you pass the actual class instead of a class name. This way you don't have to dig into globals() to try to find the right class based on the name.
Change the last line in the above to look like this:
self.show_frame(MainView)
You will also need to change get_page, but it's now a simple lookup:
def get_page(self, page_class):
return self.frames.get(page_class, None)
The final step is to redefine show to create the frame on demand. You've created a method called instantiate_page, but I see no real reason not to put it all in a single function since it's only a couple extra lines of code:
def show(self, page_class):
if page_class in self.frames:
frame = self.frames[page_class]
else
frame = page_class(parent=self.container, controller=self)
frame.grid(row=0, column=0, sticky="nsew")
self.frames[page_class] = frame
frame.tkraise()
That's all there is to it. You just need to remember to pass the class rather than the name of the class when calling show or get_page (eg: controller.show(listVPN), controller.get_page(pickFiles)`, etc)
What im trying to do is find a way to change the Frame from a method/function other than def __init__
My main class which im using to change and manage the Frames with is:
class ShopCounter(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 = {}
for i in (PageOne, PageTwo):
frame = i(container, self)
self.frames[i] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.showFrame(Login)
def showFrame(self, cont):
frame = self.frames[cont]
frame.tkraise()
The issue im having from here is that when i make a new window such as
class PageOne (tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self, parent)
lbl = tk.Label(self, text="Page 1")
lbl.pack()
This is also in __init__
as this calls the nextPage function (Later shown)
nextPage()
But i need to read from a CSV spread sheet to check if the person has inputted the user name and pass word (Haven't put checking code or input fields for convenience) i then find that i need to change the frame from a different function in the class PageOne
I have then met the problem of now having to define a controller in this new function s through the parenthesis of the function when it is called which is a problem because i don't know how to do that apart from just putting it as a parameter in the __init__method
Quick example:
Say the function in the class is something like this..
def nextPage (self, controller):
controller.showFrame(PageTwo)
IDLE then tells me that i the arguments for this function "controller" is missing but i don't know how I'd set that manually
I have found a fix
class PageOne (tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self, parent)
lbl = tk.Label(self, text="Page 1")
lbl.pack()
button = tk.Button(self, text="switch", command=lambda: self.launch(controller)
button.pack()
def launch (self, cont):
cont.showFrame(PageTwo)
Now I've made a function (launch) in the PageOne class which takes self and cont (the controller) as the parameters. You will then pass the controller from init into the launch function when you press the button.
Hope this helps some on. 'Cause personally I've been stumped on this one for months.
UPDATE
After months of this answer being up, i have now found a new way, rather than always passing the controller as a parameter to each function you with to use in each page class, in the initiation of the class (__ init __), you can declare a variable to the object that holds the controller, heres an example:
class PageTwo(tk.Frame):
def __init__ (self, parent, controller):
self.controller = controller
def doSomething (self):
self.controller.doSomething()
by referencing the controller you can run any class within the controller class, you can also get variable data and change it.