难以理解python中的Tkinter mainloop()
好的,我有一个基本窗口,带有编辑和查看按钮。按照我的代码,EDIT和VIEW都会返回一条消息“这个按钮没用”。我在“main_window”类下创建了这些。我创建了另一个类“edit_window”,我希望在单击edit按钮时调用它。基本上,单击“编辑”按钮应该会更改显示带有“添加”和“删除”按钮的新窗口。这是我到目前为止的代码…下一个逻辑步骤是什么难以理解python中的Tkinter mainloop(),python,python-2.7,tkinter,Python,Python 2.7,Tkinter,好的,我有一个基本窗口,带有编辑和查看按钮。按照我的代码,EDIT和VIEW都会返回一条消息“这个按钮没用”。我在“main_window”类下创建了这些。我创建了另一个类“edit_window”,我希望在单击edit按钮时调用它。基本上,单击“编辑”按钮应该会更改显示带有“添加”和“删除”按钮的新窗口。这是我到目前为止的代码…下一个逻辑步骤是什么 from Tkinter import * #import the Tkinter module and it's methods #create
from Tkinter import *
#import the Tkinter module and it's methods
#create a class for our program
class main_window:
def __init__(self, master):
frame = Frame(master)
frame.pack(padx=15,pady=100)
self.edit = Button(frame, text="EDIT", command=self.edit)
self.edit.pack(side=LEFT, padx=10, pady=10)
self.view = Button(frame, text="VIEW", command=self.view)
self.view.pack(side=RIGHT, padx=10, pady=10)
def edit(self):
print "this button is useless"
def view(self):
print "this button is useless"
class edit_window:
def __init__(self, master):
frame = Frame(master)
frame.pack(padx=15, pady=100)
self.add = Button(frame, text="ADD", command=self.add)
self.add.pack()
self.remove = Button(frame, text="REMOVE", command=self.remove)
self.remove.pack()
def add(self):
print "this button is useless"
def remove(self):
print "this button is useless"
top = Tk()
top.geometry("500x500")
top.title('The Movie Machine')
#Code that defines the widgets
main = main_window(top)
#Then enter the main loop
top.mainloop()
只需创建一个框架,而不是使用框架
:
class MainWindow:
#...
def edit(self):
EditWindow()
class EditWindow(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.add = Button(self, text="ADD", command=self.add)
self.remove = Button(self, text="REMOVE", command=self.remove)
self.add.pack()
self.remove.pack()
我已经根据CapWords约定更改了类名(请参阅)。这不是强制性的,但我建议您在所有Python项目中使用它,以保持统一的样式。关于mainloop,您还不了解什么?在我看来,这似乎是你在寻找的,你可以用它把编辑窗口变成一个单独的窗口。我可能解释得不够好。我希望主窗口根据您单击的按钮进行更新。因此,如果单击“编辑”,我不想打开新窗口,我希望现有窗口完全显示“编辑和视图”--它应该更新为显示“添加”“删除”,与以前显示“编辑视图”的方式相同。有道理?所以我假设Toplevel只需使用ADD-REMOVE打开一个新窗口。感谢您的回答!不过,我试图让程序在一个窗口中运行。顶层窗口很烦人。在删除“编辑”视图中的按钮时,是否有方法添加“添加”“删除”按钮?要更新窗口,可以这么说吗?更新:我意识到我可以隐藏根窗口并强制聚焦顶层窗口,所以看起来窗口不会改变。谢谢你的帮助!