为什么滚动条不起作用?

时间:2017-10-09 15:16:09

标签: python user-interface tkinter scrollbar

这是我的TKinter GUI代码:

from tkinter import *;
from tkinter import ttk
# import tix as tk

class pryprotclass:

    def onFrameConfigure(self, event,canvas1):
       canvas1.configure(scrollregion=canvas1.bbox("all"))

    def __init__(self,master):

       frame2=Frame(master);
       frame2.configure(background='yellow')
       frame2.pack_propagate(False) ;
       frame2.pack(fill="both", expand=True,side=RIGHT);
       canvas1 = Canvas(frame2, borderwidth=0, background="#ffffff")
       canvas1.configure(scrollregion=canvas1.bbox("all"))
       frame3=Frame(canvas1);

       vsb = Scrollbar(frame3, orient="vertical", command=canvas1.yview)
       canvas1.configure(yscrollcommand=vsb.set)
       vsb.pack(side="right", fill="y")
       canvas1.pack(side="left", fill="both", expand=True)
       canvas1.create_window((4,4), window=frame3, anchor="nw")
       canvas1.configure(scrollregion=canvas1.bbox("all"))
       frame3.bind("<Configure>"), 
       canvas1.configure(scrollregion=canvas1.bbox("all"))
       frame3.config( width=240 );
       frame3.configure(background='purple')
       frame3.pack_propagate(False) ;

现在大约有40个标签可以占据整个y方向

见图片 - https://imgur.com/Epq6vJJ

代码中的错误是什么?

滚动条既不工作也不能看到滚动条中的栏向上或向下移动?

这是请求的代码(不必要的)

    label=Label(frame3,text='ALL TASKS')
    label.configure(background="WHITE",font="Courier 14 bold")
    label.pack(side=TOP,anchor="nw",padx=10,pady=10)

    chromelogo=PhotoImage(file="./Images/chrome.gif")
    chromelogo=chromelogo.subsample(16,16)
    labelchrome=Label(frame3,text="Chrome")
    labelchrome.config(image=chromelogo)
    labelchrome.config(background='WHITE')
    labelchrome.config(compound='left')
    labelchrome.image=chromelogo
    labelchrome.pack(side=TOP,anchor="nw")
    c1=Checkbutton(frame3,text="History")
    c2=Checkbutton(frame3,text="Cookies")
    c3=Checkbutton(frame3,text="Saved Passwords")
    c4=Checkbutton(frame3,text="Download History")
    c5=Checkbutton(frame3,text="Last Download Location")
    c1.config(background='WHITE')
    c2.config(background='WHITE')
    c3.config(background='WHITE')
    c4.config(background='WHITE')
    c5.config(background='WHITE')
    c1.pack(anchor="nw",padx=30)
    c2.pack(anchor="nw",padx=30)
    c3.pack(anchor="nw",padx=30)
    c4.pack(anchor="nw",padx=30)
    c5.pack(anchor="nw",padx=30)


    firefoxlogo=PhotoImage(file="./Images/firefox.gif")

    firefoxlogo=firefoxlogo.subsample(18,18)
    labelfirefox=Label(frame3,text="Firefox")
    labelfirefox.config(background='WHITE')

    labelfirefox.config(image=firefoxlogo)
    labelfirefox.config(compound='left')
    labelfirefox.image=firefoxlogo
    labelfirefox.pack(side=TOP,anchor="nw")
    c6=Checkbutton(frame3,text="History")
    c7=Checkbutton(frame3,text="Cookies")
    c8=Checkbutton(frame3,text="Saved Passwords")
    c9=Checkbutton(frame3,text="Internet Cache")
    c10=Checkbutton(frame3,text="Saved Form Information")
    c6.config(background='WHITE')
    c7.config(background='WHITE')
    c8.config(background='WHITE')
    c9.config(background='WHITE')
    c10.config(background='WHITE')
    c6.pack(anchor="nw",padx=30)
    c7.pack(anchor="nw",padx=30)
    c8.pack(anchor="nw",padx=30)
    c9.pack(anchor="nw",padx=30)
    c10.pack(anchor="nw",padx=30)


    iexplorelogo=PhotoImage(file="./Images/iexplore.gif")

    iexplorelogo=iexplorelogo.subsample(12,12)
    labeliexplore=Label(frame3,text="Internet Explorer")
    labeliexplore.config(image=iexplorelogo)
    labeliexplore.config(background='WHITE')

    labeliexplore.config(compound='left')
    labeliexplore.image=iexplorelogo
    labeliexplore.pack(side=TOP,anchor="nw")
    c11=Checkbutton(frame3,text="History")
    c12=Checkbutton(frame3,text="Cookies")
    c13=Checkbutton(frame3,text="Last Download Location")
    c14=Checkbutton(frame3,text="Temporary Internet Files")
    c15=Checkbutton(frame3,text="Autocomplete Form History")
    c11.config(background='WHITE')
    c12.config(background='WHITE')
    c13.config(background='WHITE')
    c14.config(background='WHITE')
    c15.config(background='WHITE')
    c11.pack(anchor="nw",padx=30)
    c12.pack(anchor="nw",padx=30)
    c13.pack(anchor="nw",padx=30)
    c14.pack(anchor="nw",padx=30)
    c15.pack(anchor="nw",padx=30)
    explorerlogo=PhotoImage(file="./Images/explorer.gif")
    explorerlogo=explorerlogo.subsample(16,16)
    explorerlabel=Label(frame3,text="Windows Explorer")
    explorerlabel.config(image=explorerlogo)
    explorerlabel.config(background='WHITE')
    explorerlabel.config(compound='left')
    explorerlabel.image=explorerlogo
    explorerlabel.pack(side=TOP,anchor="nw")
    c16=Checkbutton(frame3,text="Recent Documents")
    c17=Checkbutton(frame3,text="Run(in Start Menu)")
    c18=Checkbutton(frame3,text="Network Passwords")
    c16.config(background='WHITE')
    c17.config(background='WHITE')
    c18.config(background='WHITE')
    c16.pack(anchor="nw",padx=30)
    c17.pack(anchor="nw",padx=30)
    c18.pack(anchor="nw",padx=30)
    systemlogo=PhotoImage(file="./Images/system.gif")

    systemlogo=systemlogo.subsample(16,16)
    systemlabel=Label(frame3,text="System")
    systemlabel.config(image=systemlogo)
    systemlabel.config(background='WHITE')
    systemlabel.config(compound='left')
    systemlabel.image=systemlogo
    systemlabel.pack(side=TOP,anchor="nw")
    c19=Checkbutton(frame3,text="Recycle Bin")
    c20=Checkbutton(frame3,text="Temporary Files")
    c21=Checkbutton(frame3,text="Clipboard")
    c22=Checkbutton(frame3,text="DNS Cache")
    #c23=Checkbutton(frame3,text="add")
    c19.config(background='WHITE')
    c20.config(background='WHITE')
    c21.config(background='WHITE')
    c22.config(background='WHITE')
    c19.pack(anchor="nw",padx=30)
    c20.pack(anchor="nw",padx=30)
    c21.pack(anchor="nw",padx=30)
    c22.pack(anchor="nw",padx=30)
    frame3.pack(fill="y", expand=False,side="left");
    frame4=Frame(frame2);
    frame4.configure(background='PINK')
    frame4.pack_propagate(False) ;
    frame5=Frame(frame4)
    frame5.configure(background='green')
    progressbar=ttk.Progressbar(frame4,orient=HORIZONTAL)
    progressbar.pack(fill=BOTH,padx=20,pady=15)
    run_PryPro=Button(frame4,text="RUN")

    frame5.configure(height=300)
    frame5.pack(fill=BOTH,expand=True,padx=20)
    run_PryPro.pack(padx=60,pady=25,ipadx=20,ipady=10)
    frame4.packfill="both", expand=True,side="right");

主要类别的代码==

from tkinter import * ;
from lefttoolbar import *;
from pyprot import *;
from logotop import *;
root=Tk()
obj_pryprot=pryprotclass(root);
=root.minsize(800, 500);

root.mainloop();

1 个答案:

答案 0 :(得分:0)

在编写代码时需要更有条理。我在你的代码中至少计算了九个错误。您需要首先创建尽可能少的小部件,然后仅在您知道现有功能正常工作时才添加功能。一次调试八件事非常困难。

以下是需要解决的问题:

  • PEP8建议类名以大写字符
  • 开头
  • 您需要删除frame3.pack_propagate(False)。关闭几何体传播是一项您几乎不应该使用的高级功能。
  • 您需要删除frame2.pack_propagate(False)
  • 您需要将滚动条设为frame2而非frame3的子项,因为frame3是您要滚动的内容。作为一般规则,滚动条和它们滚动的内容应该具有相同的父/主
  • 您需要为画布指定尺寸
  • 您需要将canvas1设为实例变量
  • 您的绑定需要将函数与事件相关联
  • 绑定函数需要采用单个参数
  • 绑定功能需要使用self.canvas1而不是canvas1

这是代码的一个版本,修复了所有这些问题:

from tkinter import *;

class Pryprotclass:

    def onFrameConfigure(self, event):
        self.canvas1.configure(scrollregion=self.canvas1.bbox("all"))

    def __init__(self,master):

       frame2=Frame(master);
       frame2.pack(fill="both", expand=True, side=RIGHT);

       self.canvas1 = Canvas(frame2, borderwidth=0, background="#ffffff")
       vsb = Scrollbar(frame2, orient="vertical", command=self.canvas1.yview)
       self.canvas1.configure(yscrollcommand=vsb.set)

       self.canvas1.pack(side="left", fill="both", expand=True)
       vsb.pack(side="right", fill="y")

       frame3=Frame(self.canvas1);
       self.canvas1.create_window((4,4), window=frame3, anchor="nw")
       frame3.bind("<Configure>", self.onFrameConfigure), 

       # dummy widgets for testing
       for i in range(100):
           label = Label(frame3, text="Item #%s" % i)
           label.pack(side="top", fill="x", padx=10, pady=1)



root = Tk()
p = Pryprotclass(root)
root.mainloop()