我的目标是制作根据画布大小而变化的 GUI。我需要能够主动检查窗口大小,以便知道何时显示附加内容。使用 Python 3.8.2 GuiZero
问问题
469 次
3 回答
1
您可以在画布上使用tkinter
事件:<Configure>
def on_resize(event):
print(app.height)
...
app.tk.bind('<Configure>', on_resize)
于 2020-03-26T01:01:14.223 回答
0
我终于能够做点什么,但在应用程序退出后它确实会抛出错误。
w=None
while True:
x=app.tk.winfo_height()
if x!=w:
print(x)
w=app.tk.winfo_height()
app.update()
于 2020-03-25T20:58:35.057 回答
0
我在创建数字蚀刻草图时遇到了这个问题,使用 Raspberry Pi 和两个电位器作为水平和垂直控件。如何获取画布的当前大小?令人讨厌的是,当您将高度和宽度设置为“填充”然后尝试询问这些值时,您得到的只是“填充”,如果您尝试确定可用画布的上限,这将毫无用处。我深入研究了对象层次结构,发现 .tk_winfo_height() 和 .tk.winfo_width() 返回整数值。为此,我删除了对电位器旋转做出反应的代码,并在屏幕底部放置了一排按钮来控制垂直和水平移动。
from guizero import App, Box, Drawing, PushButton
x = 0
y = 0
def clear_screen():
drawing.clear()
def move_left():
global x, y
if x > 0 :
drawing.line(x, y, x - 1, y)
x = x - 1
def move_right():
global x, y
if x < drawing.tk.winfo_width() :
drawing.line(x, y, x + 1, y)
x = x + 1
def move_up():
global x, y
if y > 0 :
drawing.line(x, y, x, y - 1)
y = y - 1
def move_down():
global x, y
if y < drawing.tk.winfo_height() :
drawing.line(x, y, x, y + 1)
y = y + 1
app = App()
drawing = Drawing(app, height="fill", width="fill")
drawing.bg="white"
bbox = Box(app, align="bottom")
lbtn = PushButton(bbox, align="left", command=move_left, text="Left")
ubtn = PushButton(bbox, align="left", command=move_up, text="Up")
cbtn = PushButton(bbox, align="left", command=clear_screen, text="Clear")
rbtn = PushButton(bbox, align="left", command=move_right, text="Right")
dbtn = PushButton(bbox, align="left", command=move_down, text="Down")
app.display()
于 2020-06-15T14:29:33.803 回答