这是我的问题:我用tkinter在python中编写GUI,当然不是专家,它变得越复杂,我发现的问题就越多。所以我在这里。 下面的小脚本是我主应用程序的一部分,仍然会产生我的问题。我在主包含框架中有几个子框架。在第一个子帧中,两个按钮可以调用类来填充另一个子帧。
我的问题是当我想使用"本地视图"按下底部框架f3与笔记本(从pmw包),后者似乎被切割,好像f3无法扩展。如果我通过按下"全局视图"来填充中间子帧。按钮,它扩展了f3而不扩展笔记本。
然而:
你知道我的笔记本会发生什么吗?谢谢!
要测试脚本,请使用第37和39行的IF语句在f4子帧内部和下面切换,以及第61和66行的IF语句在笔记本和帧之间切换(如果需要更多详细信息,请询问我必需的)。
import sys,os
if sys.version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
import Pmw
class MainApp():
def __init__(self, master):
self.master = master
self.mainf = tk.LabelFrame(self.master, text="main")
self.mainf.grid(row=0, column=0, padx = 10, pady = 10, sticky='nsew')
self.f1 = tk.LabelFrame(self.mainf, text="f1")
self.f1.grid(row=0, column=0, padx = 10, pady = 10, sticky='nsew')
self.f2 = tk.LabelFrame(self.mainf, text="f2")
self.f2.grid(row=1, column=0, padx = 10, pady = 10, sticky='nsew')
self.f3 = tk.LabelFrame(self.mainf, text="f3")
self.f3.grid(row=2, column=0, padx = 10, pady = 10, sticky='nsew')
### Buttons
self.selButton = tk.Button(self.f1, text = 'local view', width=15, command=self.set_local_view)
self.selButton.grid(row=0, column=0, padx = 10, pady = 10)
self.allButton = tk.Button(self.f1, text = "global view", width=15, command=self.set_global_view)
self.allButton.grid(row=0, column=1, padx = 10, pady = 10)
## Close function
self.master.wm_protocol("WM_DELETE_WINDOW", self.exitProgram)
def set_local_view(self):
if 1: # add the notebook in the main frame
DetailMon(self.mainf)
if 0: # add the notebook in a subframe
DetailMon(self.f3)
def set_global_view(self):
PreviewMon(self.f2)
def exitProgram(self):
print("Exit Button pressed")
self.master.destroy()
class PreviewMon():
def __init__(self, master):
self.master = master
self.f1 = tk.LabelFrame(self.master, text="P1", width=700, height=50)
self.f1.grid(row=0, column=0, padx = 5, pady = 5, sticky='nsew')
self.f1.grid_propagate(False)
class DetailMon():
def __init__(self, master):
self.master = master
if 1: # Create, grid and add a page to the notebook
self.notebook = Pmw.NoteBook(self.master)
self.notebook.grid(row=3, column=0, padx = 10, pady = 10, sticky='nsew')
self.page = self.notebook.add('Manual')
if 0: # Create a labelframe instead of a notebook
self.page = tk.LabelFrame(self.master, text="manual")
self.page.grid(row=0, column=0, padx = 5, pady = 5, sticky='nsew')
self.group1 = tk.LabelFrame(self.page, text='f1')
self.group1.grid(row=0, column=0, padx = 10, pady = 1, sticky='nsew')
# Create a scale
tk.Scale(self.group1, from_=10, to=40, length=600, tickinterval=5, orient='horizontal').pack()
def main():
root = tk.Tk()
app = MainApp(root)
root.mainloop()
if __name__ == '__main__':
main()