繁体   English   中英

如何在 tkinter with.bind 和<configure></configure>

[英]How to dynamically wrap label text in tkinter with .bind and <Configure>

我正在尝试创建一个输出大量数据的页面,并根据 window 大小动态包装文本。 我首先设置wraplength = self.master.winfo_width() ,它将文本换行设置为当前的 window 大小,但是当 window 这样做时它不会改变。 我找到了这个答案,这似乎可以解决问题,但是当我尝试自己重新创建它时,出了点问题。 我怀疑我对.bind<Configure>有误解,但我不能确定。 我的基本代码如下:

from tkinter import *

class Wrap_example(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.place(relx=0.5, anchor='n')
#Initalize list and variable that populates it  
        self.data_list = []
        self.data = 0
#Button and function for creating a bunch of numbers to fill the space
        self.button = Button(self, text = "Go", command = self.go)
        self.button.grid()
    def go(self):
        for self.data in range(1, 20000, 100):
            self.data_list.append(self.data)
#Label that holds the data, text = list, wraplength = current window width       
        self.data = Label(self, text = self.data_list, wraplength = self.master.winfo_width(), font = 'arial 30')
        self.data.grid()
#Ostensibly sets the label to dynamically change wraplength to match new window size when window size changes
        self.data.bind('<Configure>', self.rewrap())
    def rewrap(self):
        self.data.config(wraplength = self.master.winfo_width())
        
frame01 = Wrap_example()
frame01.mainloop()

有几点需要注意:我尝试直接使用lambda ,如链接答案所示,但它没有用。 如果我删除重新包装 function 并使用self.data.bind('<Configure>', lambda e: self.data.config(wraplength=self.winfo_width()) ,它会抛出一个通用语法错误,总是针对第一个字符在该行之后,(如果 function 保留在 def 中的 d,如果它被注释掉,则在 frame01 中的 f)。保持原样重新包装不会引发错误,但它不会执行任何其他明显的 function,要么. 单击“Go”将始终生成以当前 window 大小包装的数据,并且永远不会更改。

有几个问题:

  • 调整 window 的大小时,frame Wrap_example不会填充所有水平空间
  • label self.data没有填充框架内的所有水平空间Wrap_example调整框架大小时
  • self.rewrap()将在执行self.data.bind('<Configure>', self.rewrap())立即执行

要解决上述问题:

  • self.place(...)中设置relwidth=1
  • 调用self.columnconfigure(0, weight=1)
  • 使用self.data.bind('<Configure>', self.rewrap) (在rewrap之后没有 () )并在rewrap()中添加event参数
from tkinter import *

class Wrap_example(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.place(relx=0.5, anchor='n', relwidth=1)  ### add relwidth=1
        self.columnconfigure(0, weight=1) ### make column 0 use all available horizontal space
#Initalize list and variable that populates it
        self.data_list = []
        self.data = 0
#Button and function for creating a bunch of numbers to fill the space
        self.button = Button(self, text = "Go", command = self.go)
        self.button.grid()
    def go(self):
        for self.data in range(1, 20000, 100):
            self.data_list.append(self.data)
#Label that holds the data, text = list, wraplength = current window width
        self.data = Label(self, text = self.data_list, wraplength = self.master.winfo_width(), font = 'arial 30')
        self.data.grid()
#Ostensibly sets the label to dynamically change wraplength to match new window size when window size changes
        self.data.bind('<Configure>', self.rewrap) ### remove () after rewrap
    def rewrap(self, event): ### add event argument
        self.data.config(wraplength = self.master.winfo_width())

frame01 = Wrap_example()
frame01.mainloop()

暂无
暂无

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

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