Tkinter滚动条占用的空间超出预期

2024-04-19 17:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些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()

Tags: texttopbuttonleftframesidelabelpack
1条回答
网友
1楼 · 发布于 2024-04-19 17:31:18

举这样一个例子不会给你很多答复。一个字母的变量和非结构化的方法使程序既好又不可读。我真的建议你以后使用一种更结构化的方法。类在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帮忙解决包装问题。。。你知道吗

相关问题 更多 >