我使用Tkinter制作带有某些功能的MenuBar,我想在MenuBar下面的同一窗口中显示它们。下面的代码可以这样做。但问题是当我来回切换功能时,它与前一个功能显示重叠,你仍然可以看到标签(或条目),并能够输入上一个功能的条目。
有没有办法在每次运行函数之前刷新显示?像cls一样。
import Tkinter
from Tkinter import Tk, Frame, Menu
from Tkinter import IntVar, BOTH
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Test")
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
menubar.add_cascade(label="Discrepancy", menu=fileMenu)
fileMenu.add_command(label="View current case", command=self.view)
fileMenu.add_command(label="OK Process", command=self.OK)
def view(self):
self.pack(fill=BOTH, expand=1)
self.var = IntVar()
Tkinter.Label(self,text="RMA", relief=Tkinter.RIDGE).grid(row=0, column=0, sticky=Tkinter.W)
Tkinter.Label(self, text="SN", relief=Tkinter.RIDGE).grid(row=0, column=1, sticky=Tkinter.W)
def OK(self):
self.pack(fill=BOTH, expand=1)
self.var = IntVar()
Tkinter.Label(self,text="RMA").grid(row=0)
Tkinter.Label(self, text="SN").grid(row=1)
Tkinter.Entry(self, width=50).grid(row=0, column=1,pady=4)
Tkinter.Entry(self, width=50).grid(row=1, column=1,pady=4)
def main():
root = Tk()
root.geometry("1280x720")
app = App(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
使用grid_forget()
隐藏元素。
您可以在新Frame
中添加元素,然后您只需对此框架使用grid_forget()
。
您甚至可以在开头创建所有小部件,然后使用grid_forget()
隐藏并grid(...)
再次显示。
修改强>
import Tkinter
from Tkinter import Tk, Frame, Menu
from Tkinter import IntVar, BOTH
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Test")
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
menubar.add_cascade(label="Discrepancy", menu=fileMenu)
fileMenu.add_command(label="View current case", command=self.view)
fileMenu.add_command(label="OK Process", command=self.OK)
self.var = IntVar()
self.view_label1 = Tkinter.Label(self, text="RMA", relief=Tkinter.RIDGE)
self.view_label2 = Tkinter.Label(self, text="SN", relief=Tkinter.RIDGE)
self.ok_label1 = Tkinter.Label(self,text="RMA")
self.ok_label2 = Tkinter.Label(self, text="SN")
self.ok_entry1 = Tkinter.Entry(self, width=50)
self.ok_entry2 = Tkinter.Entry(self, width=50)
self.pack(fill=BOTH, expand=1)
def view(self):
self.ok_label1.grid_forget()
self.ok_label2.grid_forget()
self.ok_entry1.grid_forget()
self.ok_entry2.grid_forget()
self.view_label1.grid(row=0, column=0, sticky=Tkinter.W)
self.view_label2.grid(row=0, column=1, sticky=Tkinter.W)
def OK(self):
self.view_label1.grid_forget()
self.view_label2.grid_forget()
self.ok_label1.grid(row=0)
self.ok_label2.grid(row=1)
self.ok_entry1.grid(row=0, column=1, pady=4)
self.ok_entry2.grid(row=1, column=1, pady=4)
def main():
root = Tk()
root.geometry("1280x720")
app = App(root)
root.mainloop()
if __name__ == '__main__':
main()
Frame
编辑
import Tkinter
from Tkinter import Tk, Frame, Menu
from Tkinter import IntVar, BOTH
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Test")
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
menubar.add_cascade(label="Discrepancy", menu=fileMenu)
fileMenu.add_command(label="View current case", command=self.view)
fileMenu.add_command(label="OK Process", command=self.OK)
self.var = IntVar()
self.frame_view = Frame(self)
self.view_label1 = Tkinter.Label(self.frame_view, text="RMA", relief=Tkinter.RIDGE)
self.view_label2 = Tkinter.Label(self.frame_view, text="SN", relief=Tkinter.RIDGE)
self.view_label1.grid(row=0, column=0, sticky=Tkinter.W)
self.view_label2.grid(row=0, column=1, sticky=Tkinter.W)
#self.frame_view.pack(fill=BOTH, expand=1)
self.frame_ok = Frame(self)
self.ok_label1 = Tkinter.Label(self.frame_ok, text="RMA")
self.ok_label2 = Tkinter.Label(self.frame_ok, text="SN")
self.ok_entry1 = Tkinter.Entry(self.frame_ok, width=50)
self.ok_entry2 = Tkinter.Entry(self.frame_ok, width=50)
self.ok_label1.grid(row=0)
self.ok_label2.grid(row=1)
self.ok_entry1.grid(row=0, column=1, pady=4)
self.ok_entry2.grid(row=1, column=1, pady=4)
#self.frame_ok.pack(fill=BOTH, expand=1)
self.pack(fill=BOTH, expand=1)
def view(self):
self.frame_ok.grid_forget()
self.frame_view.grid()
def OK(self):
self.frame_view.grid_forget()
self.frame_ok.grid()
def main():
root = Tk()
root.geometry("1280x720")
app = App(root)
root.mainloop()
if __name__ == '__main__':
main()