簡體   English   中英

如何在 label 小部件中使用滾動條在多行上顯示長字符串(段落)? (tkinter)

[英]How can i display a long string (paragraphs) in a label widget on multiple lines with a y scrollbar? (tkinter)

嘿,我試圖將包含幾段的文本放入 label 小部件中。 我希望它顯示在多行上並使用滾動條,以便所有內容都可以在 label 小部件中顯示和查看。

結果應該是這樣的:除了文本小部件之外,它將是 label 小部件

我嘗試使用以下代碼執行此操作:

from tkinter import *

writtenSolution = Tk()
writtenSolution.title("Written Response Question")
writtenSolution.resizable(0,0)

header = LabelFrame(writtenSolution, bg="white")
content = LabelFrame(writtenSolution, bg="white")

header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space
homeButton=Button(content,width=50,height=50)
try:
    homeIcon=PhotoImage(file="yes.png")
    homeButton.config(image=homeIcon)
    homeButton.image = homeIcon
except TclError:
    print("Home")
homeButton.grid(row=1, sticky="w", padx=15, pady=2)

titleHeader = Label(content, text="Solution Image here",pady=15, padx=20, bg="white", font=("Ariel",20, "bold"), anchor="w", relief="solid", borderwidth=1)
titleHeader.grid(row=2, column=0, columnspan=3, padx=15, pady=5, ipadx=400, ipady=150)

text=str("CRXdJdRpVPJKzs2HZ 5YadBSDgsmMrWb OTsTkHoy23ygHrryEwNGvkabnM3UOJnM0PVvVU6KiXtDbvljdILbfwko1 R3dKSz6iTbKQHlHYqtFMB4xBclX5Jgc9Q8ThoXX292SO8JzWxaLq6jyfXItZx4g2 JQtAOaEtc4jSx643aLaqeRdyoqqvT1mzsrE5pa8EXMozajbRp5DvGpVanvym1BZFcdLmZEu1ykHgAGL4luFQYdoHTJ033Q3Btuu4oqRdAo4zAdPWVKOLGd316w7Os3H26eB2qQXxyMOfgzMWjWZD")

#the label widget is below:

answerInput = Label(content, borderwidth=2, font=("HelvLight", 18), height=10, width=60, text=text, relief="solid", bg="white")
answerInput.grid(row=3, column=0, sticky="w", padx=(20,0), pady=20)
#answerScrollBar= Scrollbar(content, command=answerInput.yview) #command code sets the scrollbars function so it can scroll vertically
#answerInput.configure(yscrollcommand=answerScrollBar.set)
#answerScrollBar.grid(row=3, column=1, sticky="ns", pady=20)

nextButton = Button(content, borderwidth=1, font=("Ariel", 22), text=("Next"+"\n"+"Question"), bg="#12a8e3", fg="black", activebackground="#12a8e3", relief="solid")
nextButton.grid(row=3, column=2, ipady=50, ipadx=70, sticky="nw", pady=20, padx=5)

header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')

writtenSolution.mainloop()

字符串存儲在文本變量中

解決方案是在state disabled的情況下復制並粘貼您的文本。

要將小部件設為只讀,您可以將 state 選項從 NORMAL 更改為 DISABLED:

     text.config(state=NORMAL)
     text.delete(1.0, END)
     text.insert(END, text)
     text.config(state=DISABLED)

請注意,您必須先將 state 更改回 NORMAL,然后才能從程序中修改小部件內容。 否則,對插入和刪除的調用將被靜默忽略。

更多的

向您展示一個可執行示例:

import tkinter as tk

root = tk.Tk()

def show_it(event):
    text = write_me.get('1.0','end') #get the text
    write_me.delete('1.0','end') #delet the text
    write_me.index('insert')
    
    show_me.config(state='normal')
    show_me.insert('end', text)
    show_me.config(state='disabled')

write_me = tk.Text(root)
write_me.pack(side='left')
write_me.bind('<Return>', show_it)

show_me = tk.Text(root,state='disabled')
show_me.pack(side='right')

root.mailoop()

另請注意,您可以這樣以進一步簡化它:

show_me = tkst.ScrolledText(root,state='disabled') #if you have python 3.7+

簡單的方法可以做到這一點。將其設為 label 並輸入\n進入該行

from tkinter import *
my_window = tk.Tk()

label_1 = Label(my_window, text="Line of Text\nSecond Line of Text\nThird line of text",font="Times 14"
label_1.pack()

my_window.mainloop()

請投票如果它有效

您可以只使用三個逗號,只需在三個逗號內輸入文本。

“””type anything

you want here.”””

使用三個逗號,您可以根據需要添加任意多的空格或行。

暫無
暫無

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

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