亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過新窗口更改 tkinter 畫布的大???

如何通過新窗口更改 tkinter 畫布的大???

元芳怎么了 2023-07-27 16:11:29
所以我有一個帶有畫布的 Tkinter 屏幕。我想通過創建一個具有條目小部件的新窗口來更改畫布的大小。所以我創建了一個新屏幕并添加了 2 個條目小部件。我想從這些小部件中獲取值,并基于此......它應該改變畫布的大小。我嘗試這樣做一個小時,但沒有成功。請幫助我。這是我的代碼from tkinter import *# create root window root = Tk()# Create Canvascanvas = Canvas(root, width=50, height=50)# Create an additional window (the one that is used to enter the new geometry)dialog = Toplevel(root)# Add entry widgets for width and height to the new windowwidth_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 = Button(dialog, text = 'Apply geometry', command = lambda: canvas.geometry(width_entry.get()+'x'+height_entry.get()))# Its not possible to get the geometry of a canvas in tkinter...so how do I change the size.# display the entry boxes and buttonwidth_entry.pack()height_entry.pack()apply_button.pack()# start the tk mainlooproot.mainloop()請幫助我
查看完整描述

1 回答

?
慕村225694

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 個以內。


如果您還有其他問題等,請告訴我們。


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號