【问题标题】:Tkinter Message widget does not take all the horizontal spaceTkinter Message 小部件不占用所有水平空间
【发布时间】:2021-06-19 00:51:02
【问题描述】:

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

程序代码:

    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)

输出:

【问题讨论】:

  • 尝试将padxpady 设置为零。它们是添加到小部件外部的外部填充。
  • 不,没有什么大的区别(只是左边的填充消失了)
  • anchor='c'?
  • 尝试改用LabelText 小部件。您必须自己用前者包装文本(但使用 textwrap 模块很容易)。

标签: python tkinter


【解决方案1】:

您需要为您的消息框设置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()

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多