Tkinter消息小部件不占用所有水平空间

2024-09-28 22:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的应用程序末尾创建一个帮助菜单。我也使用了Message小部件。我的窗口基本上是一个有两列的网格。我已经将消息小部件放在了末尾,并跨越了两列,但是由于某种原因,小部件并没有占据整个水平空间。有没有办法强迫它占据所有的水平空间(同时确保文本换行显示在下面)

程序代码:

    self.helpDesc = """Help:
1. The URL textbox specified the URL to be watched. In order to verify whether the given URL is valid according to the program one could wait for the status code of the URL to appear in the next line.
2. Testing Again
    """

    self.helpBox = Message(self, text=self.helpDesc, anchor='w')
    self.helpBox.grid(row=10, column=0, columnspan=2, sticky=N+S+W+E, padx=8, pady=8)

输出:

Output


Tags: thetoself应用程序网格消息urlmessage
1条回答
网友
1楼 · 发布于 2024-09-28 22:26:09

您需要为messagebox设置一个width参数。这些通常使用像素值,而标签使用预期的字符数。如果需要的话,我相信有一种方法可以改变这些,但我不记得那是什么。不管怎样,这都会让你更接近你的目标

#! /usr/bin/env python3
import tkinter as tk

Title  = 'Text Wrap'
Width, Height = 600, 150
root  = tk .Tk() ; root .title( Title )
root .bind( '<Escape>',  lambda e: root .destroy() )
root .geometry( f'{Width}x{Height}' )

helpDesc = """Help:
1. The URL textbox specified the URL to be watched. In order to verify whether the given URL is valid according to the program one could wait for the status code of the URL to appear in the next line.
2. Testing Again
"""

lt = tk .Label( root, text='this', bg='white', width=int(Width/16) ) .grid( row=0, column=0 )
rt = tk .Label( root, text='that', bg='grey', width=int(Width/16) ) .grid( row=0, column=1 )

helpBox = tk .Message( root, text=helpDesc, width=Width, anchor='w' ) .grid( row=1, column=0, columnspan=2, sticky=tk.W )

lb = tk .Label( root, text='the', bg='grey', width=int(Width/16) ) .grid( row=2, column=0 )
rb = tk .Label( root, text='other', bg='white', width=int(Width/16) ) .grid( row=2, column=1 )

root .mainloop()

相关问题 更多 >