【问题标题】:replacing a new interface with old one in tkinter用 tkinter 中的旧界面替换新界面
【发布时间】:2018-03-19 09:48:02
【问题描述】:

我创建了一个类,它有一个名为 mainScreen() 的函数。它只是打印带有两个按钮的主屏幕。如果您按下任何按钮,它必须转到另一个名为 signup() 的函数。我想清除整个框架并创建新的小部件,但我无法清除小部件

class graphics:
    def __init__(self, master):
    self.root = master


    def mainscreen(self):
        helv36 = tkFont.Font(family='Century Gothic', size=20)
        mainFrame = Frame(self.root)
        mainFrame.config(relief='sunken', width=1280, height=720, bg='light 
blue')
        mainFrame.pack(expand='yes', fill='both')
        inButton = Button(mainFrame, text = "Sign up", bd = 10, relief = 
GROOVE, font = helv36)
        inButton.bind("<Button-1>", self.signup)
        inButton.place(bordermode = OUTSIDE, width =160, height = 60, x = 
600, y = 300)
        upButton = Button(mainFrame, text = "Sign in", bd = 10, relief = 
GROOVE, font = helv36)
        upButton.bind("<Button-1>", self.signup)
        upButton.place(bordermode = OUTSIDE, width =160, height = 60, x = 
600, y = 400)
        mainFrame.pack_propagate(FALSE)
        self.root.mainloop()

    def signup(self,event):
        signUpShow = Frame(self.root)
        signUpShow.config(relief='sunken', width=1280, height=720, bg='light 
yellow')
        signUpShow.pack(expand='yes', fill='both')

【问题讨论】:

  • 为什么不能清除小部件?是什么阻止你这样做?
  • 在绑定函数中,保持被传递给另一个函数 singup(),我无法从 signup() 访问 mainFrame 变量,因为它是 mainScreen() 的本地变量

标签: python-2.7 user-interface tkinter


【解决方案1】:

您的__init__ 需要缩进其代码,并且需要调用mainscreenmainFramemainscreen 中的本地化解决方案是使其也成为一个属性。

self.mainframe = mainFrame = Frame(self.root)

那么你就可以在signup中访问self.mainframe了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多