1 回答

TA貢獻1880條經(jīng)驗 獲得超4個贊
您正在尋找的命令是canvas.config
在這里,我調(diào)整了給定的代碼:
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 導(dǎo)入了 *,但對于某些項目,您仍然以tk.;開頭。我將它們?nèi)扛臑槠ヅ?,并將?dǎo)入也更改為匹配。(您仍然可以使用 *,但只是沒有前導(dǎo)tk.s。)
畫布從來沒有被打包過,所以你永遠看不到那里發(fā)生了什么。
還有一個建議,制作按鈕的那一行真的很長。也許創(chuàng)建一個函數(shù)來完成 lambda 所做的事情,并將其命令分配給該函數(shù)而不是 lambda。您可能會發(fā)現(xiàn),如果有人(可能是您自己的未來版本)嘗試閱讀您的代碼,并編輯它或理解它,那么這么長的行甚至很難閱讀。一般來說,盡量將所有行的字符數(shù)控制在 80 個以內(nèi)。
如果您還有其他問題等,請告訴我們。
添加回答
舉報