所以我有 2 行专门用于消息标签小部件,以在用户使用 tkinter GUI 时向用户显示任何成功/不成功的消息。有些消息太长而无法适应列宽,因此我使用了 Label 小部件的 wraplength 功能将文本换行到下一行。
我遇到的问题是,由于此功能,它会将小部件在此小部件下方的位置移动,对于将文本包装到的每个新行,它会移动 1 行。
所以我想知道是否有任何方法可以让文本换行,而无需移动下部小部件。
消息标签在将文本换行到高度 = 1 的换行符时的外观:
我希望第二个图像链接中的消息标签显示它的方式,但保持小部件的垂直布局,如第一个图像链接中所示。
以下代码用于定义小部件:
Choice_list = Listbox(window, selectmode=SINGLE, width = 17, height = 9,
justify = CENTER)
image = PhotoImage(file = 'Dummy_Logo.gif')
Image_label = Label(window, image = image)
extract = Button(window, text = "Archive News from Webpage",
command = func1, width = 20)
archive = Button(window, text = "Extract News from Archive",
command = func2, width = 22)
display = Button(window, text = "Display the News",
command = func3, width = 14)
Message_Widget = Label(window, text = '', fg = 'black', justify = CENTER,
height = 2, wraplength = 300)
Log_Event = Checkbutton(window, text = 'Log Event', variable = logState,
command = logEvent)
以下代码是小部件的网格对齐方式:
Image_label.grid(row = 1, column = 1)
Choice_list.grid(row = 1, column = 2, rowspan = 9, sticky = W)
Message_Widget.grid(row = 2, column = 1, rowspan = 2)
Log_Event.grid(row = 12, column = 2)
archive.grid(row = 13, column = 1, rowspan = 2, sticky = W)
extract.grid(row = 13, column = 1, rowspan = 2, sticky = E)
display.grid(row = 13, column = 2, rowspan = 2, sticky = W)