【发布时间】:2022-01-20 21:12:30
【问题描述】:
所以我正在尝试在 pygame 中创建一个 GUI,但过了一段时间,我决定在 tkinter 中重新编写它。我需要创建 3 个条,一个是画布,另一个是空的。左侧栏的大小为leftbarwidth,右侧为rightbarwidth,分别设置为200 和300。到目前为止我有这个(不包括程序的逻辑)
1 from tkinter import *
2
3 def windowsizeupdate(event):
4 preview.config(width=event.width-leftbarwidth-rightbarwidth, height=event.height)
5
6 root = Tk()
7 root.title("TITLE HERE")
8 root.geometry(f"{leftbarwidth + rightbarwidth + 200}x600+{int(screen.winfo_screenwidth() / 2 - (leftbarwidth + rightbarwidth + 200) / 2)}+{int(screen.winfo_screenheight() / 2 - 300)}")
9 root.minsize(800, 600)
10 root.attributes("-topmost", 1)
11
12 preview = Canvas(root, bd=0, bg="#800000", cursor="dot", width=200, height=600)
13 preview.place(x=leftbarwidth, y=0)
14
15 root.bind("<Configure>",windowsizeupdate)
16 root.mainloop()
但是每当我运行它时,我都会得到这个:
在注释掉第 4 行并将其替换为 print(event.width-leftbarwidth-rightbarwidth, event.height, leftbarwidth) 后,我发现
一切正常,所有值都应该与 config 函数一起使用!
但是如果我输入这个:
1 from tkinter import *
2
3 def windowsizeupdate(event):
4 print(event.width-leftbarwidth-rightbarwidth, event.height, leftbarwidth)
5 preview.config(width=event.width-leftbarwidth-rightbarwidth, height=event.height)
6
7 root = Tk()
8 root.title("TITLE HERE")
9 root.geometry(f"{leftbarwidth + rightbarwidth + 200}x600+{int(screen.winfo_screenwidth() / 2 - (leftbarwidth + rightbarwidth + 200) / 2)}+{int(screen.winfo_screenheight() / 2 - 300)}")
10 root.minsize(800, 600)
11 root.attributes("-topmost", 1)
12
13 preview = Canvas(root, bd=0, bg="#800000", cursor="dot", width=200, height=600)
14 preview.place(x=leftbarwidth, y=0)
15
16 root.bind("<Configure>",windowsizeupdate)
17 root.mainloop()
我看到我的控制台输出如下所示:
【问题讨论】:
-
请不要发布带有行号的代码,这会使我们难以复制和粘贴您的代码。