[英]TKInter global variable for button
I use a multiple frame window from Steven M. Vascellaro example Switch between two frames in tkinter . 我使用Steven M. Vascellaro的示例中的多帧窗口在tkinter中的 两个帧之间切换 。 And I don't understand is it possible to use global variable, because each frame is destroyed every time it is changed. 而且我不知道是否可以使用全局变量,因为每次更改时每个框架都会被破坏。 1st (of 2 buttons) button at StartPage
is deactivated, if I click on 2nd button and go to PageOne
and do something there and return to StartPage 1st button must be activated. StartPage
第一个按钮(共2个按钮)被停用,如果我单击第二个按钮并转到PageOne
并在那里做某事并返回StartPage,则必须激活第一个按钮。
If I declare global
in StartPage
class 如果我在StartPage
类中声明global
_tkinter.TclError: invalid command name ".!startpage.!button2"
If I declare it in PageOne
class 如果我在PageOne
类中声明
NameError: name 'install_svc' is not defined
Not good idea, but I think about 3rd frame where I define buttons again. 这不是一个好主意,但是我想到了第三帧,我再次定义了按钮。
EDIT1: 编辑1:
class MyGUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
self.geometry("400x300")
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
def quit_app(self):
self._frame.destroy()
self.destroy()
class StartPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
global install_svc
class PageOne(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
def entry_check(self,parent):
global install_svc
Declare and initialize install_svc
at the root level (global scope). 在根级别(全局范围)声明并初始化install_svc
。 The statement global install_svc
wont auto-magically create, by itself, the variable in the global scope: it will just do a lookup for a global variable with such name but, if you try to use that variable without first assigning something to it, a NameError
will be thrown. 语句global install_svc
本身不会自动神奇地创建全局范围内的变量:它只会查找具有此类名称的全局变量,但是,如果您尝试使用该变量而不先给它分配任何东西, NameError
将被抛出。
install_svc = None # Or some other initial value
class MyGUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
self.geometry("400x300")
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
def quit_app(self):
self._frame.destroy()
self.destroy()
class StartPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
global install_svc
# You can use install_svc safely
print(install_svc)
class PageOne(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
def entry_check(self,parent):
global install_svc
# You can use install_svc safely
print(install_svc)
You could also check if the variable exists, doing a try..except
, but that will be really ugly (at least for me) and you will need to do such check everywhere: 您还可以try..except
来检查变量是否存在,但这确实很丑陋 (至少对我而言),并且您需要在所有地方进行此类检查:
class MyGUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
self.geometry("400x300")
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
def quit_app(self):
self._frame.destroy()
self.destroy()
class StartPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
global install_svc
try:
install_svc
except NameError:
install_svc = None # Or some initial value
class PageOne(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
def entry_check(self,parent):
global install_svc
try:
install_svc
except NameError:
install_svc = None # Or some initial value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.