简体   繁体   English

如何通过新窗口更改 tkinter 画布的大小?

[英]How do I change the size of a tkinter canvas through a new window?

So I have one Tkinter screen that has a canvas.所以我有一个带有画布的 Tkinter 屏幕。 I want to change the size of the canvas by creating a new window that has entry widgets.我想通过创建一个具有条目小部件的新窗口来更改画布的大小。 So I created a new screen and added 2 entry widgets.所以我创建了一个新屏幕并添加了 2 个条目小部件。 I want to get the value from those widgets and based on that...it should change the size of the canvas.我想从这些小部件中获取值并基于此...它应该更改画布的大小。 I tried to do this for an hour, but no luck.我试图这样做一个小时,但没有运气。 Please assist me.请帮助我。

Here is my code这是我的代码

from tkinter import *
# create root window 
root = Tk()

# Create Canvas
canvas = 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 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 = 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 button
width_entry.pack()
height_entry.pack()
apply_button.pack()

# start the tk mainloop
root.mainloop()

Please Assist me请帮助我

The command you are looking for is canvas.config您正在寻找的命令是canvas.config

Here, I have adjusted the given code:在这里,我调整了给定的代码:

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()

I also changed a couple other things:我还改变了一些其他的东西:

You imported * from tkinter, but for some items you still led with tk.您从 tkinter 导入了 *,但对于某些项目,您仍然使用tk.引导tk. ; ; I changed them all to match that and switched the import to match as well.我将它们全部更改为匹配,并将导入也切换为匹配。 (You could still use *, but then just don't have the leading tk. s.) (您仍然可以使用 *,但只是没有领先的tk.

The canvas was never packed so you could never see what was going on there.画布从来没有打包,所以你永远看不到那里发生了什么。

One more suggestion, that line where you make the button is really long.还有一个建议,你制作按钮的那一行真的很长。 Maybe make a function that does what the lambda does and assign its command to that function instead of a lambda.也许创建一个函数来执行 lambda 的操作并将其命令分配给该函数而不是 lambda。 You can probably see that a line that long is even hard to read here much less if someone (maybe a future version of yourself) was to try to read your code, and edit it or make sense of it.您可能会看到,如果有人(可能是您的未来版本)试图阅读您的代码、编辑它或理解它,那么长的一行在这里甚至很难阅读。 Generally, try to keep all lines down to 80 characters.通常,尽量将所有行保持在 80 个字符以内。

Let us know if you have any more questions etc.如果您还有其他问题等,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM