2017-10-09 227 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()