2017-10-09 230 views
-4

这是我使用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方向为什么滚动条不起作用?

见image-- 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(); 
+0

你昨天问同样的问题... – DavidG

+0

https://stackoverflow.com/questions/46624488/why-is-the-scrollbar-not-working -in-heretkinterpython-3-4 – DavidG

+0

为什么你认为滚动条应该工作?画布上只有一个项目,一个小框架。没有什么可滚动的。 –

回答

0

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

这里是需要固定的东西:

  • PEP8建议类名称以大写字母
  • 您需要删除frame3.pack_propagate(False)开始。关闭几何形状传播是一种几乎从不应该使用的高级功能。
  • 您需要删除frame2.pack_propagate(False)
  • 您需要进行滚动孩子frame2,不frame3,因为frame3是要滚动的东西。作为一般规则,滚动条和他们滚动应该有相同的父的事/主
  • 你需要给画布大小
  • 你需要canvas1是可变的实例
  • 你的绑定需要关联一个与事件功能
  • 结合的功能需要采取一个参数
  • 绑定的功能需要使用self.canvas1而非canvas1

这里有一个已经所有的这些问题,你的代码rsion固定:

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()