Tkinter Scrollbar占用的空间超出预期

时间:2015-04-04 12:16:33

标签: python tkinter scrollbar frames

我在使用tkinter时遇到了一些问题,我有两个滚动条,第一个不适合整个框架,正在采用子框架(f5),这是一个问题,我试图制作一个新的框架,并改变现有的框架,但我得到的只是垃圾,我将对滚动条问题提供任何帮助。

#!/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()

1 个答案:

答案 0 :(得分: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提供包装问题......