I am trying to add a scroll bar to a text box which makes up part of my GUI.
So far I have made the text box and (i think) the scroll bar but dont know how to combine the two items.
textBox_1 = Text(myGUI).place(x=75, y=300)
scroll_1 = Scrollbar(myGUI)
scroll_1.configure()
应该:
textbox_1.config(yscrollcommand=scroll_1.set)
When using Tkinter
, no matter what geometry manager you're using, you need to create your widget and use the geometry manager on separate lines if you want to keep a reference to the Widget. In other words, Widget.place
returns None
(as does Widget.pack
and Widget.grid
).
textBox_1 = Text(myGUI)
textBox_1.place(x=75, y=300)
scroll_1 = Scrollbar(myGUI)
textbox_1.config(yscrollcommand=scroll_1.set)
There are two steps that you need to take: you need to connect the scrollbar to the widget, and you need to connect the widget to the scrollbar. For example:
textBox_1 = Text(...)
scroll_1 = Scrollbar(...)
textBox_1.configure(yscrollcommand=scroll_1.set)
scroll_1.configure(command=textBox_1.yview)
Also, I notice that you called place
as part of widget creation. You cannot do that. When you do Text(...).place(...)
it stores the result of place
in textbox_1
, not the result of Text(...)
. Plus, it's just easier to maintain your code when the layout is separate from widget creation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.