问题
Hey im trying to put text that contains a few paragraphs in a label widget. I would like it to display on multiple lines and use a y scrollbar so everything can fit and be viewed in the label widget.
It should turn out like this: Except instead of a text widget it would be a label widget
I have tried to do this with the following code:
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()
the string is stored in the text variable
回答1:
The solution is to copy and paste your text in another with the state disabled.
To make the widget read-only, you can change the state option from NORMAL to DISABLED:
text.config(state=NORMAL)
text.delete(1.0, END)
text.insert(END, text)
text.config(state=DISABLED)
Note that you must change the state back to NORMAL before you can modify the widget contents from within the program. Otherwise, calls to insert and delete will be silently ignored.
more
to show you an executable example:
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()
Also note you can do this to simplify it some more:
show_me = tkst.ScrolledText(root,state='disabled') #if you have python 3.7+
来源:https://stackoverflow.com/questions/63412427/how-can-i-display-a-long-string-paragraphs-in-a-label-widget-on-multiple-lines