簡體   English   中英

如何在 Python 中格式化標簽(或其他小部件)中的文本位置?

[英]How do I format the text placement in a label (or other widget) in Python?

所以我有一個帶有很長字符串的變量作為它的值。 此字符串將在新窗口中顯示給用戶,以便他們可以將其復制並粘貼到程序中。 因此,變量的值顯示在彈出的新窗口中的標簽中。 問題是,變量的文本都是水平的並且在一行中,所以它從窗口屏幕上消失了,這看起來非常煩人,並且更難以復制。 有沒有辦法讓它格式化,以便它在窗口上帶有滾動條垂直運行?

我發現使用文本框效果更好 - 這里是網站: 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM