我正在开发 Tkinter GUI,我想在其单独的类中添加窗口大小和定位控制。 我的结构是这样的:
class MainApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.winsize= WinSize(container, 700, 500, 650, 450, True, True)
我希望 WinSize 设置主应用程序的几何形状以及句柄 WinSize 类中的 minsize、resizeable。
class WinSize:
def __init__(self, container, width, height, minx, miny, sizeablex, sizeabley):
self.container = container
self.width = width
self.height = height
self.minx = minx
self.miny = miny
self.sizeablex = sizeablex
self.sizeabley = sizeabley
self.ws = self.container.winfo_screenwidth()
self.hs = self.container.winfo_screenheight()
self.xpos = (self.ws / 2) - (self.width / 2)
self.ypos = (self.hs / 2) - (self.height / 2)
我一直在广泛搜索,但没有找到关于如何在 WinSize 类中为特定实例和/或框架实现这些的解决方案/指南。 我想使用相同的类来设置大小和其他属性以显示其他信息的弹出消息/框架。 主要的 GUI 类取自@Bryan Oakley 的著名示例:https://stackoverflow.com/a/7557028/7703610
如何从 Tk 中调用 geometry、minsize 和 resizeable 而不必在我的 WinSize 类中再次继承 Tk 然后应用它到那个特定的实例?
请您参考如下方法:
有两种解决方案。您可以将窗口传递给 WinSize() 而不是容器,或者您可以使用 winfo_toplevel获取特定小部件窗口的方法。
第一个解决方案如下所示:
class MainApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
...
self.winsize= WinSize(self, 700, 500, 650, 450, True, True)
class WinSize:
def __init__(self, window, width, height, minx, miny, sizeablex, sizeabley):
self.window = window
...
self.window.geometry("%d%d+%d+%d" % (width, height, minx, miny))
...
第二种解决方案不需要更改主应用程序。只需将以下内容添加到 WinSize:
class WinSize:
def __init__(...):
...
window = self.container.winfo_toplevel()
self.window.geometry("%d%d+%d+%d" % (width, height, minx, miny))
