Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在交换帧程序中使用StringVar将挂起_Python_Tkinter - Fatal编程技术网

Python 在交换帧程序中使用StringVar将挂起

Python 在交换帧程序中使用StringVar将挂起,python,tkinter,Python,Tkinter,根据文章中的示例,我创建了一个切换帧应用程序。当我在框架页面中使用StringVar作为变量时,我修改为下面的代码,UI不会显示,代码在后台运行 from tkinter import * from tkinter import font as tkfont import time import threading class RMS_APP(Tk): def __init__(self, *args, **kwargs): Tk.__init__(self, *ar

根据文章中的示例,我创建了一个切换帧应用程序。当我在框架页面中使用StringVar作为变量时,我修改为下面的代码,UI不会显示,代码在后台运行

from tkinter import *
from tkinter import font  as tkfont
import time
import threading

class RMS_APP(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
        self.title("Restaurant Management System")
        self.master = Frame(self)
        self.master.grid(row=0,column=0,sticky='nwes')
        #self.geometry("1600x800+0+0")

        #self.master.value = StringVar()
        self.master.grid_rowconfigure(0, weight=1)
        self.master.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (LogIn,):#,Kitchen):
            page_name = F.__name__
            frame = F(parent=self.master, 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("LogIn")

    def show_frame(self, page_name,arg = None):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]


        if arg:
            #frame.OnClick(arg)
            #self.label1.grid_forget()
            frame.label1['text'] = arg
            pass
        frame.tkraise()

class LogIn(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        self.localtime=time.asctime(time.localtime(time.time()))
        label = Label(self, text="Restaurant Management System", font=controller.title_font)
        label.grid(row=0,column=0,sticky='news',columnspan=3,padx = 500,pady=(150,10))
        lblinfo = Label(self, font=controller.title_font,text=self.localtime,fg="black",anchor=W)
        lblinfo.grid(row=1,column=0,columnspan=3,padx=500,pady=(0,10))
        button1 = Button(self, text="Table",height = 10, width = 15,
                            command=lambda: controller.show_frame("Table"))
        button2 = Button(self, text="Kitchen",height = 10, width = 15,
                            command=lambda: controller.show_frame("Kitchen"))
        button3 = Button(self, text="Bill Table",height = 10, width = 15,
                            command=lambda: controller.show_frame("Kitchen"))
        button1.grid(row=2,column=0,sticky='news',padx=(250,10),pady = (100,10))
        button2.grid(row=2,column=1,sticky='news',padx=(0,10),pady = (100,10))
        button3.grid(row=2,column=2,sticky='news',pady = (100,10))
if __name__ == "__main__":
    app = RMS_APP()
    app.mainloop()

当我取消注释self.master.value行时,代码没有启动。通过注释,我可以启动代码,并显示登录页面。是否还有其他需要检查的内容

我已尝试通过减少代码来查找问题,但找不到任何内容。如果我取消注释
StringVar
行,此代码将不会显示窗口:

from tkinter import *

class RMS_APP(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.master = Frame(self)

        self.my_integer = 4     # Works!
        #self.my_stringvar = StringVar()    # Works not!

RMS_APP().mainloop()
但是如果我从
Frame
继承我的类,它会工作:

class RMS_APP(Frame):
我不知道为什么这是

更新
Tk
Frame
都已具有属性“master”。和RMS_APP.master=='.'。当你改变这一点时,事情显然会破裂。

self.master.value意味着Tk obejct(应用程序)有一个函数
value
,但事实并非如此。将它设置为一个变量,而不是将
master
重命名
self.master
为任何其他变量,它就会工作
tk.tk
有一个同名的内部属性,我也看不出来这很奇怪。@JoshuaNixon:哦,我现在明白了,这是因为
self.master
是一个内部属性,我们不应该更改它,因为子小部件依赖于
tk
窗口的
master
。太愚蠢了,这是有史以来最糟糕的部分。谢谢你们的帮助。