【发布时间】:2016-05-03 17:37:13
【问题描述】:
在没有任何正式培训和对 Python 3.3 非常基本的了解的情况下,我正在努力理解 tkinter 以制作基本的 gui。我已经阅读了这种方法:
Tkinter! Understanding how to switch frames
这超出了我的想象。我从复制和粘贴代码中学得不好。我的问题是这样的:
如果我想制作一个带有多个窗口的 gui,我通过简单地销毁一个框架并在使用类时用另一个框架替换它来实现,我想我只是不明白为什么它如此困难。今天玩了一会儿,不明白为什么下面的代码不起作用。我不能把我的面条包裹起来是为什么它可以正确翻转到新窗口,但无法返回主窗口。两个按钮的编码相同,用于各自的目的,但一个有效,一个无效不。
from tkinter import *
class Application:
def __init__(self, master):
self.master = master
self.new_switch_on = False
self.main_switch_on = False
self.main_frame()
def main_frame(self):
if self.new_switch_on == True:
self.new_frame.destroy()
self.new_switch_on = False
self.main_switch_on = True
self.main_frame = Frame(self.master, width = 200, height = 100)
Label(self.main_frame, text = 'Main Frame').pack()
Button(self.main_frame, text = 'Next', command = self.new_frame).pack()
self.main_frame.pack()
def new_frame(self):
if self.main_switch_on == True:
self.main_frame.destroy()
self.main_switch_on = False
self.new_switch_on = True
self.new_frame = Frame(self.master, width = 400, height = 200)
Label(self.new_frame, text = 'New Frame').pack()
Button(self.new_frame, text = 'Back', command = self.main_frame).pack()
self.new_frame.pack()
root = Tk()
root.geometry('-1+1')
app = Application(root)
root.mainloop()
我知道我可以按照上述链接获得所需的结果,但我只是不明白为什么这样的事情,或者对于新学习者来说更简单的其他事情不能达到相同的结果。
【问题讨论】:
标签: tkinter frame python-3.3