How to attach a Scrollbar to a Text widget?

前端 未结 3 756
北荒
北荒 2020-11-28 07:54

I am trying to attach a scrollbar to my Text field and have been unable to do so. Here is the segment of code:

self.scroller = Scrollbar(self.root)
self.scro         


        
3条回答
  •  心在旅途
    2020-11-28 08:57

    If you're working with an INPUT box, then a handy way using the scrolledtext function. It took me 4+ hours to find it. Don't ya love tkinter?

    Two things to note... The additional import required import tkinter.scrolledtext as tkscrolled and you set default value using insert and read the value using get (more terrible naming)

    This bit of code was central to making my 20 character wide by 10 lines text input box to work.

    import tkinter.scrolledtext as tkscrolled
    import tkinter as tk
    
    default_text = '1234'
    width, height = 20,10
    TKScrollTXT = tkscrolled.ScrolledText(10, width=width, height=height, wrap='word')
    
    # set default text if desired
    TKScrollTXT.insert(1.0, default_text)
    TKScrollTXT.pack(side=tk.LEFT)
    

    The scroll bars show up once reaching the height as defined in the call. They are grayed out and blend into the background well. It works great..... once you figure out the correct calls to make.

    I hope this is relevant to your question!

提交回复
热议问题