如何让滚动条工作?

时间:2017-05-11 10:16:22

标签: python tkinter scrollbar

我和Tkinter一起工作,我需要这个命令"滚动条"。滚动窗口小部件已显示但我无法滚动它。知道为什么吗?这是我用于滚动的代码:

from Tkinter import *
import Tkinter as tk
master = tk.Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
master.geometry('1000x500')
master.title('test')
master.mainloop()

1 个答案:

答案 0 :(得分:0)

试试这个:

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

#Note that you don't have to import tkinter as tk after you have used from tkinter import *

master=Tk() #you don't have to type tk.Tk() because you have imported all of tkinter's
#commands in your main script

def myScrollcmd(event): #You need to bind this function to your canvas so that your scroll 
#bar works
    mycanvas.config(scrollregion=mycanvas.bbox('all'))   


mycanvas = Canvas(master)
mycanvas.pack(fill=BOTH, expand=True)
myFrame = Frame(mycanvas)
mycanvas.create_window((0, 0), window=myFrame, anchor=NW)
myScrollbar = Scrollbar(mycanvas, orient=VERTICAL, command=mycanvas.yview)
myScrollbar.pack(side=RIGHT, fill=Y)
mycanvas.config(yscrollcommand=myScrollbar.set)
mycanvas.bind("<Configure>", myScrollcmd)

for x in range(100):
    Label(myFrame, text="Text "+str(x)).pack()

master.mainloop()

这对我有用。我使用Python 3.它也应该适合你。如果没有,请阅读文档并在互联网上搜索适用于您的python版本的有效tkinter命令。您的代码与此类似,但您必须修改我的代码以满足您的需求。

祝你好运