1 回答
![?](http://img1.sycdn.imooc.com/545862aa0001f8da02200220-100-100.jpg)
TA贡献1880条经验 获得超4个赞
您正在寻找的命令是canvas.config
在这里,我调整了给定的代码:
import tkinter as tk
# create root window
root = tk.Tk()
# Create Canvas
canvas = tk.Canvas(root, width=50, height=50)
canvas.pack()
# Create an additional window (the one that is used to enter the new geometry)
dialog = tk.Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = tk.Button(dialog, text = 'Apply geometry', command = lambda: canvas.config(width=width_entry.get(), height=height_entry.get()))
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
我还改变了其他一些事情:
您从 tkinter 导入了 *,但对于某些项目,您仍然以tk.;开头。我将它们全部更改为匹配,并将导入也更改为匹配。(您仍然可以使用 *,但只是没有前导tk.s。)
画布从来没有被打包过,所以你永远看不到那里发生了什么。
还有一个建议,制作按钮的那一行真的很长。也许创建一个函数来完成 lambda 所做的事情,并将其命令分配给该函数而不是 lambda。您可能会发现,如果有人(可能是您自己的未来版本)尝试阅读您的代码,并编辑它或理解它,那么这么长的行甚至很难阅读。一般来说,尽量将所有行的字符数控制在 80 个以内。
如果您还有其他问题等,请告诉我们。
添加回答
举报