2015-04-04 96 views
0

我有一些tkinter的問題,我有兩個滾動條,第一個不適合整個框架,採取子幀(f5),這是一個問題,我已經嘗試過創建一個新的框架並改變現有的框架,但我所得到的只是垃圾,我會對滾動條問題提供任何幫助。Tkinter滾動條佔用比預期更多的空間

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
#spyder 
from Tkinter import * 
import tkFileDialog 


def curdir(): 
    cdir = tkFileDialog.askdirectory(parent=finestra, initialdir="/home") 
    v.set(cdir) 

#MAIN 
finestra = Tk() 
finestra.title("Creacio de fitxer comprimit") 
f=Frame(finestra) 
f.pack(side=TOP) 

b=Button(f,text='Escollir directori treball',command=curdir) 
b.pack(side=LEFT,anchor=W) 

v=StringVar() 
v.set("/home") 
e1=Entry(f,width=35,textvariable=v) 

e1.pack(side=LEFT) 

l1=Label(f,text="Fitxers a incorporar al fitxer tar:") 
l1.pack(side=TOP,anchor=N,padx=120) 

f1=Frame(finestra) 
f1.pack(side=TOP,anchor=NW) 

l2=Label(f1,text="Llista:") 
l2.pack(side=LEFT) 

br=Button(f1,text='Reomplir') 
br.pack(side=LEFT) 
bo=Button(f1,text='Ocultar no seleccionats') 
bo.pack(side=LEFT) 
bos=Button(f1,text='Ocultar seleccionats') 
bos.pack(side=LEFT) 

Label(f1,text="\t\tCompresió").pack(side=LEFT) 
rb1=Radiobutton(f1,text="cap").pack(side=LEFT) 
rb2=Radiobutton(f1,text="gzip",value="gzip").pack(side=LEFT) 
rb3=Radiobutton(f1,text="bzip2",value="bzip2").pack(side=LEFT) 
rb4=Radiobutton(f1,text="xz",value="xz").pack(side=LEFT) 


f2=Frame(finestra) 
f2.pack(side=LEFT,anchor=W,pady=0) 


scrollbar = Scrollbar(f2) 
scrollbar.pack(side=RIGHT,fill="y",expand=False) 

listbox = Listbox(f2, bd=0, yscrollcommand=scrollbar.set,width=55) 
listbox.pack(side=TOP,anchor=W,fill="both",expand=True) 

scrollbar.config(command=listbox.yview) 


f3=Frame(finestra) 
f3.pack(side=LEFT) 

Label(f3,text="Tots:").pack(side=TOP,anchor=W) 
tots=Button(f3,text=">>>").pack(side=TOP) 
Label(f3,text="Als seleccionats:").pack(side=TOP) 
af=Button(f3,text="-->").pack(side=TOP) 
qt=Button(f3,text="<--").pack(side=TOP) 
Label(f3,text="Tots:").pack(side=TOP,anchor=W) 
cap=Button(f3,text="<<<").pack(side=TOP) 

f4=Frame(finestra) 
f4.pack(side=TOP) 

scrollbar = Scrollbar(f4) 
scrollbar.pack(side=RIGHT, fill=Y) 

listbox = Listbox(f4, bd=0, yscrollcommand=scrollbar.set,width=35) 
listbox.pack(side=LEFT,padx=5) 

scrollbar.config(command=listbox.yview) 

f6=Frame(finestra) 
f6.pack(side=TOP,anchor=W,padx=20) 

Button(f6,text="Crea").pack(side=LEFT) 
Label(f6,text="fitxer tar:").pack(side=LEFT) 

f5=Frame(f2) 
f5.pack(side=BOTTOM,anchor=W) 
Button(f5,text="Sortir").pack(side=BOTTOM,anchor=S) 

mainloop() 

回答

0

提出這樣的例子不會得到很多答覆。單字母變量和非結構化方法使得程序不可讀。我真的建議你將來使用更結構化的方法。在Python中使用類很容易,你應該花時間練習一下。

我從來沒有編程過Tkinter(我用pygtk),並且很感興趣,所以我重寫了一部分程序。雖然可能不是完美的,我覺得(不完全)下面的代碼是靠近你需要什麼,你可能可以完成剩下的...

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# 
# tk.py 
# 

from Tkinter import * 
import tkFileDialog 
import pdb 

def curdir(): 
    cdir = tkFileDialog.askdirectory(parent=finestra, initialdir="/home") 
    v.set(cdir) 

class EscollirDireccion(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     b = Button(self, text='Escollir directori treball', command=curdir) 
     b.pack(side = LEFT) 

     v = StringVar() 
     v.set("/home") 
     e = Entry(self, width = 35, textvariable = v) 
     e.pack(side = LEFT) 


class Llista(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     l = Label(self, text = "Llista:") 
     l.pack(side = LEFT) 

     br = Button(self, text = 'Reomplir') 
     br.pack(side = LEFT) 
     bo = Button(self, text = 'Ocultar no seleccionats') 
     bo.pack(side = LEFT) 
     bos = Button(self, text = 'Ocultar seleccionats') 
     bos.pack(side = LEFT) 

class SortirButtonBox(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     b = Button(self, text = "Sortir") 
     b.pack(side = LEFT) 

     Label(self, text = "").pack(side = LEFT, expand = True) 


class ListBoxWithScrollbar(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     self.scrollbar = Scrollbar(self) 
     self.scrollbar.pack(side = RIGHT, fill = Y) 

     self.listbox = Listbox(self, bd = 0, 
            yscrollcommand = self.scrollbar.set, 
            width=55) 
     self.listbox.pack(side = RIGHT) 



class LeftPanel(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     EscollirDireccion(self).pack() 
     Llista(self).pack() 
     ListBoxWithScrollbar(self).pack() 
     SortirButtonBox(self).pack(fill = X) 


class TransferButtons(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     widgets = [(Label(self, text = "Tots"), None), 
        (Button(self, text = ">>>"), None), 
        (Label(self, text = "Als seleccionats"), None), 
        (Button(self, text = "->"), None), 
        (Button(self, text = "<-"), None), 
        (Label(self, text = "Tots"), None), 
        (Button(self, text = "<<<"), None)] 
     widgets.reverse() 
     for wdg in widgets: 
      wdg[0].pack(side = BOTTOM); 

class RightPanel(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 

     tb = TransferButtons(self).pack(side = LEFT) 

     self.listbox = ListBoxWithScrollbar(self) 
     self.listbox.pack(side = LEFT) 


class MainWindow(Frame): 
    def __init__(self, parent = None): 
     Frame.__init__(self, parent) 
     self.pack() 

     parent.title("Creacio de fitxer comprimit") 

     lp = LeftPanel(self) 
     lp.pack(side = LEFT) 
     rp = RightPanel(self) 
     rp.pack(side = LEFT) 


def main(): 
    tk = Tk() 
    mw = MainWindow(parent = tk) 
    mw.mainloop() 
    tk.destroy() 

    return 0 

if __name__ == '__main__': 
    main() 

感謝@jedwards借給手與包裝問題.. 。