python - 按下按钮时更新 tkinter 矩形尺寸
问题描述
我希望我的矩形进度条在按下按钮时改变其尺寸,特别是通过将长度增加 10。因为tk.Label
我可以使用 a textvariable
,但我不知道如何将类似的东西用于矩形坐标。
import tkinter as tk
from tkinter import ttk
def buttonUpdate(width):
return width.get()+10
root = tk.Tk()
v=tk.IntVar()
v.set(10)
tk.Button(root, text="+10", command=lambda:v.set(buttonUpdate(v))).grid()
tk.Label(root, textvariable=v).grid()
canvas=tk.Canvas(width=140, height=42)
canvas.grid(row=4, column=0, pady=2)
canvas.create_rectangle(0,0,v.get(),10,)
root.mainloop()
我怎样才能实现这样的目标?
解决方案
canvas.create_rectangle
不支持您希望的变量的使用,但我建议使用这样trace
的变量方法来实现您想要的。
def update_rectangle(canvas, r):
new = canvas.coords(r)
new[2] = new[2] + 10
canvas.coords(r, *new)
r = canvas.create_rectangle(0,0,v.get(),10)
v.trace("w", lambda a,b,c: update_rectangle(canvas, r))
root.mainloop()
您可以在此处阅读有关trace
方法的更多信息。
推荐阅读
- amazon-web-services - 通过 appsync-dynamoDB 解析器将 AWSJSON 添加到 dynamoDB 中的空 Map
- python - 将python模块输出写入toml文件的方法
- apache-camel - 骆驼 - 处理后移动到文件夹
- javascript - Ajax 仅适用于偶数个帖子
- java - 有没有办法使用 Selenium 或其他一些 java API 以编程方式创建新的命名 Firefox 配置文件?
- python - 如何使用包含布尔表达式的字符串创建 PyEDA 布尔表达式?
- r - R Shiny 情节更新分两步而不是一步
- python - 如何获得最长的子升序数组,不一定必须是连续的
- angular - 我们可以在 Angular 的根注入器中注入多少服务
- java - 为什么我在 SPOJ 上对 Aggressive Cows 的解决方案超过了时间限制?