简体   繁体   English

Python Tkinter如何使文本框滚动

[英]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.

相关问题 如何在Tkinter中制作一个闪烁的文本框? - How to make a flashing text box in tkinter? Python tkinter:使输出显示在GUI上的文本框中,而不在外壳中 - Python tkinter: Make the output appear in a text box on GUI not in the shell Python tkinter:使所有输出显示在GUI上的文本框中,而不在外壳中 - Python tkinter: Make any output appear in a text box on GUI not in the shell Python(Tkinter)单击按钮使文本出现在输入框中? - Python (Tkinter) Click button to make text appear in entry box? 在Python 2.6中使用Tkinter,如何使按钮小部件在文本框中打印出选中的Checkbuttons? - Using Tkinter in Python 2.6 how do I make a button widget print out selected Checkbuttons in a text box? Python Tkinter - 如何在文本框的开头插入文本? - Python Tkinter - How to insert text at the beginning of the text box? 文本框中的Python Tkinter侦听器 - Python Tkinter listener in text box 如何制作交互式 tkinter 文本框,然后是输入框,然后是文本框作为对输入的响应 - How do I make an interactive tkinter text box, then entry box, then text box as a response to the entry 如何在Ubuntu上的tkinter python中使文本可单击 - how to make text clickable in tkinter python on Ubuntu 如何使 tkinter 文本框成为 stdin 输入接收器? - How to make tkinter text box a stdin input receiver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM