【问题标题】:How do I format the text placement in a label (or other widget) in Python?如何格式化 Python 中标签(或其他小部件)中的文本位置?
【发布时间】:2020-09-24 18:54:48
【问题描述】:

所以我有一个带有很长字符串的变量作为它的值。该字符串将在新窗口中显示给用户,以便他们可以将其复制并粘贴到程序中。因此,变量的值显示在弹出的新窗口中的标签中。问题是,变量的文本都是水平的并且在一行中,所以它会从窗口屏幕上消失,这看起来非常烦人,并且更难以复制。有没有一种方法可以将其格式化,使其垂直运行,并在窗口上显示滚动条?

【问题讨论】:

  • 您能否提供到目前为止您尝试过的内容、您的代码或任何其他对我们有帮助的信息?
  • 您查看过所有可用的小部件吗?有大量教程展示了您可以使用的所有小部件。

标签: python user-interface tkinter text format


【解决方案1】:

我发现使用文本框效果更好 - 以下是网站: http://effbot.org/tkinterbook/text.htm

http://effbot.org/zone/tkinter-scrollbar-patterns.htm

这是我使用的代码:

from tkinter import * #Import tkinter module
import csv #Import csv module
from tkinter import messagebox

# These lines define the main/parent window
root = Tk()
root.title("Main Window")
root.geometry("500x500")
root.configure(bg='white')

def NewwindowButton():
    # The following three lines define the sub-window
    info_window = Toplevel(root)
    info_window.title("This is the info window")
    info_window.geometry("300x400")

    # These lines define a frame inside the window,
    # and the text box inside that frame with the 
    # 'variable_with_text' variable supplying the text.
    variable_with_text = "Here is the text..."
    info_window_frame = Frame(info_window, bg="white")  
    info_window_frame.place(relwidth=1.0, relheight=1.0)
    text_widget = Text(info_window, bg="white")
    text_widget.place(relwidth=1.0, relheight=1.0)
    text_widget.insert(INSERT, variable_with_text)

    # These lines define a scrollbar for the text window
    text_scrollbar = Scrollbar(info_window)
    text_scrollbar.pack(side=RIGHT, fill=Y)

    # These lines link the text widget and scrollbar together
    text_widget.config(yscrollcommand=text_scrollbar.set)
    text_scrollbar.config(command=text_widget.yview)

new_window_button = Button(root, text="Open new window", 
command=NewwindowButton, bg="grey")
new_window_button.pack()
root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2010-09-05
    • 2011-03-14
    相关资源
    最近更新 更多