Why does changing a variable from a lambda not work? - python

I have been working on Tkinter and am finding trouble passing values between different frames, so I followed this tutorial here, using the "shared data" solution provided by Bryan Oakley and adding it to my own code.
Except I cannot set the value in the "shared data" dictionary as a command on a button.
A few comments in the code below outline the problem. If I just try to change the variable during the init of my choice page, it changes normally. But putting it in a lambda means that the dictionary variable won't change at all. And trying to use a def for the button command has its own complications.
import tkinter as tk
import tkinter.ttk as ttk
# from tkinter import messagebox
TITLE_FONT = ("Segoe UI Light", 22)
SUBTITLE_FONT = ("Segoe UI Light", 12)
window_size = [300, 200]
resistors = []
choice = "default"
class RegApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="test.ico")
tk.Tk.wm_title(self, "Test")
self.shared_data = {
"choice": tk.StringVar(),
}
container = tk.Frame(self, width=window_size[0], height=window_size[1])
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 panels:
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="NSEW")
self.show_frame(WelcomePage)
def show_frame(self, container):
frame = self.frames[container]
frame.tkraise()
class WelcomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
title_label = ttk.Label(self, text="Welcome", font=TITLE_FONT)
subtitle_label = ttk.Label(self, text="Let's run some numbers.", font=SUBTITLE_FONT)
start_button = ttk.Button(self, text="Begin", width=24, command=lambda: controller.show_frame(ChoicePage))
title_label.pack(pady=(40, 5))
subtitle_label.pack(pady=(0, 10))
start_button.pack()
class ChoicePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.controller.shared_data["choice"].set("test2") # Here, the variable is set fine
title_label = ttk.Label(self, text="Is your resistor network \nin series or parallel?", font=SUBTITLE_FONT,
justify=tk.CENTER)
series_button = ttk.Button(self, text="Series", width=24,
command=lambda: [self.controller.shared_data["choice"].set("series"), controller.show_frame(ValuePage)])
# But when I use it in a lambda, the variable doesn't even seem to set at all. It switches to the next page and has the value ""
parallel_button = ttk.Button(self, text="Parallel", width=24,
command=lambda: controller.show_frame(ValuePage))
title_label.pack()
series_button.pack()
parallel_button.pack()
# TODO Make the user select between 'series' and 'parallel'
class ValuePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
title_label = ttk.Label(self, text=self.controller.shared_data["choice"].get(), font=SUBTITLE_FONT,
justify=tk.CENTER)
title_label.pack()
panels = [WelcomePage, ChoicePage, ValuePage]
app = RegApp()
app.resizable(False, False)
app.geometry('{}x{}'.format(window_size[0], window_size[1]))
app.mainloop()

well passing data between frames isn't too hard. there are 2 ways I like to do it.
Method 1:
setup a frame to be something like this....
class ThirdName(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
now just say something like:
self.data = "this is some data"
now when you're in another frame you can call it like this:
print(ThirdName.data)
>>> "this is some data"
Second way is just to send it somewhere like this:
value_1 = 'Richard'
bobby_def(value_1, 42, 'this is some text')
...
def bobby_def(name, number, text)
print(text)
or...
return(name, number, text)
Bobby will get the data :)
ok.... second point... moving between frames can be done with something like this:
self.button_to_go_to_home_page = tk.Button(self, text='Third\nPage', font=Roboto_Normal_Font,
command=lambda: self.controller.show_frame(ThirdName),
height=2, width=12, bd = 0, activeforeground=active_fg, activebackground=active_bg, highlightbackground=border_colour,
foreground=bg_text_colour, background=background_deselected)
self.button_to_go_to_home_page.place(x=20, y=280)
**set up a frame with stuff like this:
class SecondName(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
Yes, the self.controller method is one I use too and it's nice to have lots of your data together in one frame.

Why are you using 'controller' instead of 'self.controller'? It's kind of confusing that you assign 'self.controller' at the start of the constructor and then you use 'controller' instead. Maybe that variable shadowing is causing your problem.

Related

Python - accessing a local variable from another class

I have tried to condense the code down as much as possible to make it clear what I am asking...
I have a variable called chosen_name, determined in a class called booking_frame, that I would like to access in the calendar_frame class.
Therefore, it would be obvious for calendar_frame to inherit the attributes of booking_frame - however, I believe (I'm probably completely wrong lol) that calendar_frame has to inherit the characteristics of Frame so that the whole program functions correctly.
The reason that calendar_frame is a completely separate class is so that it can appear as a different frame.
Extremely grateful for any help given :)
# import tkinter modules
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
from PIL import ImageTk, Image
from tkcalendar import *
# define self
class tkinterApp(Tk):
def __init__(self,*args, **kwargs):
Tk.__init__(self, *args, **kwargs)
# creating a container
container = Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
# initialising frames to an empty array
self.frames = {}
menu_bar = Menu(container)
main_menu = Menu(menu_bar)
menu_bar.add_cascade(label="Main Menu", menu=main_menu)
main_menu.add_command(label="Welcome page", command=lambda: self.show_frame(welcome_frame))
main_menu.add_command(label="Book a vehicle", command=lambda: self.show_frame(booking_frame))
main_menu.add_command(label="Register as new user", command=lambda: self.show_frame(register_frame))
Tk.config(self, menu=menu_bar)
for F in (welcome_frame, booking_frame, register_frame, calendar_frame):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(welcome_frame)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class welcome_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
welcome = Label(self, text="Hello, please use the menu above to navigate the interface")
welcome.grid(row=0, column=4, padx=10, pady=10)
class register_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
register_label = Label(self, text="New user - enter your details below to use the Collyer's car park.")
register_label.grid()
class booking_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
chosen_name = "Steve"
class calendar_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
print(booking_frame.chosen_name)
app = tkinterApp()
app.geometry("1000x800")
app.title("Collyer's Car Park")
app.mainloop()
First you need to change local variable chosen_name to instance variable self.chosen_name inside booking_frame class, otherwise it cannot be accessed outside the class:
class booking_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.chosen_name = "Steve" # changed to instance variable
Then you can access it via controller.frames[booking_frame].chosen_name inside calendar_frame class:
class calendar_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
print(controller.frames[booking_frame].chosen_name)
Inheritance is to model relationships of objects which behave the same ("IS A" relationship). It is not meant to share data between objects.
A possible solution to your problem is to use a third object, that would be shared
between booking_frame and calendar_frame.
This object can be a Python dictionary for example ; you can pass it to all your
"frame" objects, or you can maybe decide to have it global (not very academic, for sure, but quick and dirty):
GLOBAL_STATE = {}
class booking_frame(Frame):
...
GLOBAL_STATE["chosen_name"] = "Steve"
class calendar_frame(Frame):
...
print(GLOBAL_STATE.get("chosen_name"))
I hope you can see now how you can refactor your code to share data between those objects.

Tkinter have stacking frames with list and pages in different modules

I have code below originally taken from the link here i have read all documentation in there but i feel im overlooking something, i have pages in separate modules as well as a separate module with a Add class for adding pages to the window.
The idea is to later be able to drop a module in a sub folder with a Pagexxx object within it and call the add page class to allow Tkinter to display it but i cannot seem to get the frames to stack.
Nav.py
import tkinter as tk
from Page import PageList
import Mypages
class Windowhandler(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)
PageList("Page1", container, self, Mypages.PageOne)
PageList("Page2", container, self, Mypages.PageTwo)
self.show_frame("Page1")
def show_frame(self, cont):
frameref = PageList.frames[cont]
print(frameref)
frameref.tkraise()
app = Windowhandler()
app.mainloop()
Page.py
class PageList():
frames = {}
def __init__(self, name, parent, cont, ref):
self.frames[name] = ref(parent=parent, controller=cont)
Mypages.py
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 1")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("Page2"))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 2")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("Page1"))
button1.pack()
Before asking for help from others, the first step is to validate your assumptions. You're assuming that tk.Frame.__init__(self, parent) returns something useful, but you never validated that assumption by checking to see if it is what you think it is.
The first problem is illustrated by two lines, which are essentially the same in both pages:
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 1")
I'm guessing you were assuming that this would be set to the instance of the Frame. However, the __init__ function returns None so this is set to None. When None is passed as the parent of a widget, that widget becomes a child of the root window.
The solution is to not use this. Use self:
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Welcome to Page 2")
The second problem is that you never add the page to the container with grid, pack, or place, so they will never be visible.
You need to change Add to actually add the page to the container:
def Add(name, parent, cont, ref):
PageList.frames[name] = ref(parent=parent, controller=cont)
PageList.frames[name].grid(row=0, column=0, sticky="nsew")

Python Tkinter Edit Menu Window

I'm trying to edit Menu window. How can i edit it?
This is for Windows.
from tkinter import *
Window = Tk()
MB = Menu(Window)
Window.config(menu=MB)
Menubar = Menu(MB)
MB.add_cascade(label="File", menu=Menubar)
Menubar.add_command(label="New File")
#Btn1 = Button(Menubar, width=20, text="Button").pack() #I failed.
Window.mainloop()
I tried Btn1 = Button(Menubar, width=20, text="Button").pack()
But this one couldn't work.
usually you add items to menu like this - but what are looking for?
window = Tk()
mb = Menu(window)
menu_bar = Menu(mb, tearoff=0)
menu_bar.add_command(label="New")
menu_bar.add_command(label="Open")
menu_bar.add_command(label="Save")
menu_bar.add_command(label="Save as...")
menu_bar.add_command(label="Close")
menu_bar.add_separator()
menu_bar.add_command(label="Exit", command=window.quit)
mb.add_cascade(label="File", menu=menu_bar)
top.config(menu=mb)
You cannot add arbitrary widgets to a menu. To add items to a menu you must use one of the following functions available on the Menu object:
add
add_command
add_cascade
add_checkbutton
add_radiobutton
add_separator
To add a textual label you can use add_command and set the command attribute to None.
All of these are documented with the Menu widget definition itself, and on just about any site that includes widget documentation.
If you're looking for a control of Menu Bar with Buttons you can do it. All of the code will be in the same file.py .Libraries in Python3 (For Python 2 change to import Tkinter as tk). Later you can redirect to each window with each menu with buttons.
import tkinter as tk
from tkinter import ttk
Global list of titles for
nombre_ventanas={'PageOne'}
One menubar, you need to add similar for each menu that you want to display. menubar_global, menubar1, menubar2, ...
def menubar_global(self, root, controller):
menubar_global = tk.Menu(root)
page_1 = tk.Menu(menubar_global, tearoff=0)
# With Submenu for page_1
page_1_1=tk.Menu(page_1 , tearoff=0)
page_1_2=tk.Menu(page_1 , tearoff=0)
Main
if __name__ == "__main__":
app = ControlWindow()
app.mainloop()
Class for windows control
class ControlWindow(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 = {}
self._nombre_ventanas = nombre_ventanas
for F in self._nombre_ventanas:
F = eval(F)
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
# First window
self.show_frame(Login)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# Title for each page
self.title(titulo_ventanas[cont._name_for_menu_bar])
# Here you can add if-sentece for each menubar that you want
if cont._name_for_menu_bar != "PageOne":
# Menu bar
print("show_frame", cont)
menubar = frame.menubar(self)
self.configure(menu=menubar)
else:
menubar = 0
self.configure(menu=menubar)
class Ventana_proveedores(tk.Frame):
_name_for_menu_bar = "Ventana_proveedores"
_config = configuracion
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
tk.Button(self, text='Regresar', font=FONT_LABELFRAME,
command=lambda: controller.show_frame(Ventana_gerente)).grid(row=0, column=0)
def menubar(self, root):
return menubar_global(self, root, self.controller)
Later Each Page need to have this configuration
class PageOne(tk.Frame):
_name_for_menu_bar = "PageOne"
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
def menubar(self, root):
return menubar_global(self, root, self.controller)

How to forget a child frame?

My question seems easy but after trying all methods, which should work, I could not forget or delete child frame.
Program is based on this ex: Switch between two frames in tkinter
My code:
import tkinter as tk
from tkinter import ttk
class myProgram(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.title(self, "myProgram")
framesForAllWindows = tk.Frame(self)
framesForAllWindows.pack(side="top")
framesForAllWindows.grid_rowconfigure(0)
framesForAllWindows.grid_columnconfigure(0)
self.frames = dict()
for pages in (checkPage, PageOne):
frame = pages(framesForAllWindows, self)
self.frames[pages] = frame
frame.grid(row=0, column=0, sticky="nsew")
print(framesForAllWindows.winfo_children())
self.show_frame(checkPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class checkPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.f = tk.Frame()
self.f.pack(side="top")
self.controller = controller
self.enterAppid = ttk.Label(text='Please enter your something: ', font=('Courier', 20))
self.enterAppid.pack(padx=50, pady=100)
self.appidVar = tk.StringVar()
self.appidEnter = ttk.Entry(width=60, textvariable=self.appidVar)
self.appidEnter.pack()
self.checkAppid = ttk.Button(text='check', command = self.checkInfo)
self.checkAppid.pack(pady=50, ipady=2)
def checkInfo(self):
self.appid = self.appidVar.get()
if(self.appid == "good"):
self.controller.show_frame(PageOne)
#self.f.pack_forget() doesn`t work
else:
print('Unknown Error')
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="You are on page One")
label.pack(pady=1,padx=10)
app = myProgram()
app.state('zoomed')
app.mainloop()
The goal is to forget all checkPage frame and move on to PageOne. When I execute the code that I currently have "You are on page one" appears in addition to all widgets from checkPage which I don`t want. Any ideas where I am making mistake?
You didn't specify your parent for the Label, Entry and Button. If you change these 3 lines it should work.
So like this:
self.enterAppid = ttk.Label(self.f, text='Please enter your something: ', font=('Courier', 20))
self.appidEnter = ttk.Entry(self.f, width=60, textvariable=self.appidVar)
self.checkAppid = ttk.Button(self.f, text='check', command = self.checkInfo)

3 ValueError: dictionary update sequence element #0 has length 3; 2 is required 1

I am using tkinter and trying to create a library of frames instead of having my program open new windows every time. I have begun creating a welcome page and I am trying to display what I have created only for it to give me this error message. "ValueError: dictionary update sequence element #0 has length 1; 2 is required"
Here is my code:
#!/usr/bin/python
from tkinter import *
import tkinter as tk
Large_Font = ("Verdana", 18)
class ATM(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 (WelcomePage, Checking):
frame = i(container, self)
self.frames[i] = frame
frame.grid(row= 0, column = 0, sticky= "nsew")
self.show_frame(WelcomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class WelcomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, "Welcome to the ATM Simulator", font = Large_Font)
label.pack(pady=100, padx=100)
checkButton = Button(self, text = "Checking Account",
command = lambda: controller.show_frame(Checking))
checkButton.pack()
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
self.controller = controller
label = tk.Label(self, "Welcome to the ATM Simulator", font = Large_Font)
label.pack(pady=100, padx=100)
homeButton = Button(self, text = "Back to Home Page",
command = lambda: controller.show_frame(WelcomePage))
homeButton.pack()
app = ATM()
app.mainloop()
The error message is occurring because I state that
frame = i(container, self)
but when I create the class I state
class WelcomePage(tk.Frame):
The dictionary element in my WelcomePage class only has 1 parameter but I need two. I tried putting self as the second parameter but that did not work. This worked in Python 3.4 but now that I am using Python 3.5 it gives me this error. How would I fix this?
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
I don't think Frame's initializer can accept that many arguments unless controller is a dictionary. Try:
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
You should also use the text named argument to specify the text for your labels.
label = tk.Label(self, text="Welcome to the ATM Simulator", font = Large_Font)

Categories