Python - 从另一个 class 访问局部变量
Python - accessing a local variable from another class
我已尝试尽可能地压缩代码以明确我的要求...
我有一个名为 chosen_name
的变量,在名为 booking_frame
的 class 中确定,我想在 calendar_frame
class.
因此,calendar_frame
继承 booking_frame
的属性是显而易见的 - 但是,我相信(我可能完全错了,哈哈) calendar_frame
必须继承Frame
的特性,使整个程序正常运行。
calendar_frame
是一个完全独立的 class 的原因是它可以显示为不同的框架。
非常感谢您提供的任何帮助:)
# 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()
继承是对行为相同的对象之间的关系建模(“IS A”关系)。它并不意味着在对象之间共享数据。
您的问题的一个可能解决方案是使用第三个对象,该对象将被共享
在 booking_frame
和 calendar_frame
之间。
例如,此对象可以是 Python 字典;你可以把它传递给你所有的
“框架”对象,或者你可以决定让它成为全球性的(当然不是很学术,但又快又脏):
GLOBAL_STATE = {}
class booking_frame(Frame):
...
GLOBAL_STATE["chosen_name"] = "Steve"
class calendar_frame(Frame):
...
print(GLOBAL_STATE.get("chosen_name"))
我希望您现在可以了解如何重构代码以在这些对象之间共享数据。
首先需要在booking_frame
class内将局部变量chosen_name
修改为实例变量self.chosen_name
,否则无法在class外访问:
class booking_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.chosen_name = "Steve" # changed to instance variable
然后你可以通过controller.frames[booking_frame].chosen_name
进入calendar_frame
class:
class calendar_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
print(controller.frames[booking_frame].chosen_name)
我已尝试尽可能地压缩代码以明确我的要求...
我有一个名为 chosen_name
的变量,在名为 booking_frame
的 class 中确定,我想在 calendar_frame
class.
因此,calendar_frame
继承 booking_frame
的属性是显而易见的 - 但是,我相信(我可能完全错了,哈哈) calendar_frame
必须继承Frame
的特性,使整个程序正常运行。
calendar_frame
是一个完全独立的 class 的原因是它可以显示为不同的框架。
非常感谢您提供的任何帮助:)
# 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()
继承是对行为相同的对象之间的关系建模(“IS A”关系)。它并不意味着在对象之间共享数据。
您的问题的一个可能解决方案是使用第三个对象,该对象将被共享
在 booking_frame
和 calendar_frame
之间。
例如,此对象可以是 Python 字典;你可以把它传递给你所有的 “框架”对象,或者你可以决定让它成为全球性的(当然不是很学术,但又快又脏):
GLOBAL_STATE = {}
class booking_frame(Frame):
...
GLOBAL_STATE["chosen_name"] = "Steve"
class calendar_frame(Frame):
...
print(GLOBAL_STATE.get("chosen_name"))
我希望您现在可以了解如何重构代码以在这些对象之间共享数据。
首先需要在booking_frame
class内将局部变量chosen_name
修改为实例变量self.chosen_name
,否则无法在class外访问:
class booking_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.chosen_name = "Steve" # changed to instance variable
然后你可以通过controller.frames[booking_frame].chosen_name
进入calendar_frame
class:
class calendar_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
print(controller.frames[booking_frame].chosen_name)