【问题标题】:How to resize the width of the canvas dynamically如何动态调整画布的宽度
【发布时间】:2021-12-05 11:09:30
【问题描述】:

我这里有这段代码。就像您看到其中一个按钮中的文本很长一样,它不会全部出现。是否有解决方案来调整现在定义为 300 的画布宽度,以便它将显示按钮中的所有文本。我不想手动执行,因为将来文本将从外部文件中获取,并且数据可以更改。 任何想法

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from time import strftime
import csv
import xmlrpc.client as xmlrpclib

root = Tk()
frame = Frame(root)

container = Frame(root)
canvas = Canvas(container, borderwidth = 0, highlightthickness = 0, width=300, height=500, bg = "white")
#scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)

vertibar=Scrollbar(
    container,
    orient=VERTICAL
    )
vertibar.pack(side=RIGHT,fill=Y)
vertibar.config(command=canvas.yview)

horibar=Scrollbar(
    container,
    orient=HORIZONTAL
    )
horibar.pack(side=BOTTOM,fill=X)
horibar.config(command=canvas.xview)

scrollable_frame = Frame(canvas)

# setting the minimum size of the root window
root.geometry("300x650")

# Adding widgets to the root window
Label(root, text='my app',
      font=('Verdana', 15)).pack(pady=10)

# tell the canvas how large the frame will be, so that it knows how much it can scroll:

scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)


frame_id =canvas.create_window((0, 0), window=scrollable_frame,  anchor='nw')
canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))

#canvas.configure(yscrollcommand=scrollbar.set)
canvas.config(
    xscrollcommand=horibar.set, 
    yscrollcommand=vertibar.set
    )


    # rechner_full = str(rechner[row])
    # print(rechner_full)
Button(scrollable_frame, text="text1").pack(fill="both", side="top")
Button(scrollable_frame, text="text2").pack(fill="both", side="top")
Button(scrollable_frame, text="text3").pack(fill="both", side="top")
Button(scrollable_frame, text="longtexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt").pack(fill="both", side="top")
    # print(button.cget('text'))


container.pack()
canvas.pack(side="left", fill="both")
#scrollbar.pack(side="right", fill="both")

# Button for closing
exit_button = Button(root, text="Beenden", command=root.destroy)
exit_button.pack(pady=20)


root.mainloop()

【问题讨论】:

  • 您可以使用canvas.config(width=new_value) 更改画布的宽度。可以使用 tkinter font 实例的 measure() 方法确定文本的大小。
  • 由于scrollable_frame 将被展开以显示最长的按钮,因此最好在scrollable_frame 上绑定到&lt;Configure&gt; 事件的回调内展开canvas。但是您需要删除root.geometry(...),因为它会阻止主窗口由于canvas 的大小调整而扩展。
  • @acw1668 你能用一个代码示例来解释更多是什么意思吗?谢谢

标签: python python-3.x tkinter canvas tkinter-canvas


【解决方案1】:

由于 scrollable_frame 将调整大小以显示最长的按钮,因此您可以在绑定到 scrollable_frame 上的 &lt;Configure&gt; 事件的回调内展开 canvas

...
scrollable_frame = Frame(canvas)
frame_id =canvas.create_window((0, 0), window=scrollable_frame,  anchor='nw')

def on_frame_resized(event):
    canvas_width = canvas.winfo_reqwidth()
    if event.width > canvas_width:
        # expand canvas to the width of scrollable_frame
        canvas.configure(width=event.width)
    else:
        # expand scrollable_frame to the width of canvas
        canvas.itemconfigure(frame_id, width=canvas_width)
    canvas.configure(scrollregion=canvas.bbox("all"))

# tell the canvas how large the frame will be, so that it knows how much it can scroll:
scrollable_frame.bind("<Configure>", on_frame_resized)
...

但是您需要删除root.geometry(...),因为它会阻止主窗口因canvas 的大小调整而扩大。您还需要删除canvas.bind("&lt;Configure&gt;", ...),因为它会破坏on_frame_resized() 所做的工作。

...
#root.geometry("300x650")
...
#canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))
...

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 2019-05-23
    • 1970-01-01
    • 2014-01-14
    • 2017-01-12
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多