[英]Python Tkinter how to make a text box scroll
I wrote the following code to quickly grab and display information from Wikipedia. 我编写了以下代码,以快速获取和显示Wikipedia中的信息。 It works great unless the Wiki summary contains more information than the box can display.
除非Wiki摘要包含的信息超出框显示的范围,否则它会很好用。 I thought adding sticky = N+S+E+W would fix this but it doesn't appear to be doing anything.
我以为添加粘滞= N + S + E + W可以解决此问题,但似乎没有做任何事情。 How can I update this code to make it scroll if there is too much information to display in the text box all at once?
如果有太多信息无法一次显示在文本框中,如何更新此代码以使其滚动?
enter code here 在这里输入代码
import sys
from tkinter import *
import wikipedia
def search_wiki():
txt = text.get() # Get what the user entered into the box
txt = wikipedia.page(txt) # Search Wikipedia for results
txt = txt.summary # Store the returned information
lblText = Label(main, text=txt,justify=LEFT,wraplength=600, fg='black',
bg='white', font='times 12 bold').grid(row = 50,
column = 1, sticky=N+S+E+W)
main = Tk()
main.title("Search Wikipedia")
main.geometry('750x750')
main.configure(background='ivory3')
text = StringVar()
lblSearch = Label(main, text = 'Search String:').grid(row = 0, column = 0,
padx = 0, pady = 10)
entSearch = Entry(main, textvariable = text, width = 50).grid(row = 0,
column = 1)
btn = Button(main, text = 'Search', bg='ivory2', width = 10,
command = search_wiki).grid(row = 0, column = 10)
main.mainloop()
Substitute you label with a more appropriate widget such as 用更合适的小部件代替您的标签,例如
lblText = ScrolledText(main,
bg='white',
relief=GROOVE,
height=600,
#width=400,
font='TkFixedFont',).grid(row = 50,
column = 1, sticky=N+S+E+W)
If you want to display text that is scrollable, you should use a Text
widget. 如果要显示可滚动的文本,则应使用“
Text
小部件。 You cannot scroll a Label
, and scrolling a group of Labels
is relatively difficult. 您不能滚动
Label
,并且滚动一组Labels
相对困难。 The Text
widget is by far the best choice for scrollable text. 到目前为止,“
Text
小部件是可滚动文本的最佳选择。
Thanks for all your help. 感谢你的帮助。 I finally figured everything out.
我终于弄明白了。 Here is my new code in case anybody else runs into this or something similar.
这是我的新代码,以防其他人遇到此问题或类似问题。
import sys
from tkinter import *
from tkinter import scrolledtext
from wikipedia import *
def search_wiki():
txt = text.get() # Get what the user eneterd into the box
txt = wikipedia.page(txt) # Search Wikipedia for results
txt = txt.summary # Store the returned information
global texw
textw = scrolledtext.ScrolledText(main,width=70,height=30)
textw.grid(column=1, row=2,sticky=N+S+E+W)
textw.config(background="light grey", foreground="black",
font='times 12 bold', wrap='word')
textw.insert(END, txt)
main = Tk()
main.title("Search Wikipedia")
main.geometry('750x750')
main.configure(background='ivory3')
text = StringVar()
lblSearch = Label(main, text = 'Search String:').grid(row = 0, column = 0)
entSearch = Entry(main, textvariable = text, width = 50).grid(row = 0,
column = 1)
btn = Button(main, text = 'Search', bg='ivory2', width = 10,
command = search_wiki).grid(row = 0, column = 10)
main.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.