第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

元芳怎么了 2023-07-27 16:11:29
所以我有一個帶有畫布的 Tkinter 屏幕。我想通過創(chuàng)建一個具有條目小部件的新窗口來更改畫布的大小。所以我創(chuàng)建了一個新屏幕并添加了 2 個條目小部件。我想從這些小部件中獲取值,并基于此......它應(yīng)該改變畫布的大小。我嘗試這樣做一個小時,但沒有成功。請幫助我。這是我的代碼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條經(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)。


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


查看完整回答
反對 回復(fù) 2023-07-27
  • 1 回答
  • 0 關(guān)注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號