Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter how to make a text box scroll

I wrote the following code to quickly grab and display information from Wikipedia. It works great unless the Wiki summary contains more information than the box can display. I thought adding sticky = N+S+E+W would fix this but it doesn't appear to be doing anything. 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()
like image 460
Dave Mathews Avatar asked Oct 31 '25 14:10

Dave Mathews


1 Answers

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)
like image 103
1966bc Avatar answered Nov 03 '25 05:11

1966bc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!