tkinter中的水平可滚动列使用嵌套帧

时间:2015-03-12 19:48:15

标签: python tkinter listbox grid-layout scrollable

我正在使用grid几何管理器创建一个包含以下小部件结构的表:

  • 第0列和第1列中的Radiobuttons
  • 第2列中的水平可滚动文本(表示可能很长的文件名)
  • 第6列中的
  • 和进度条(第3列的数值将在后面填写)。

这是可以横向滚动的Text给我带来麻烦。我尝试创建一个Listbox小部件,它位于第2列(作为整个框架的子级)并跨越所有行。这看起来很有希望,直到很明显列表框中的文本行与父网格的行不对齐。

我一直在寻找一种方法来填充列表框中的每一行文字,以便行匹配;但即使这是可能的,我宁愿选择更通用,更少克服的解决方案。

是否有一种简单的方法可以使单个列可滚动,而我却不知道? setGrid是一个可能的解决方案吗?

(为了查看下面代码的问题,请使用Select FileSelect Folder按钮将多个音频文件加载到文件列表中。)

#! /usr/bin/env python

#################################################
# This tool allows the user to select audio files 
# (or folders containing audio files) and subject 
# them to loudness analysis.
#################################################

import sys
import os
import codecs
import re
import Tkinter
from Tkinter import *
import tkFileDialog
from os import walk
from os import path
from Tkinter import Tk, Text, BOTH, W, N, E, S
from ttk import Frame, Button, Label, Style, Progressbar
from ScrolledText import *
from progressbar import ProgressBar

class Leveler_tk(Frame):
    fileList = []
    allowedExtensions = ['mp3','mp2','m4a','aiff','wav']

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.style = Style()
        self.style.theme_use("default")
        self.pack(fill=BOTH, expand=1)

        self.columnconfigure(1, weight=1)
        self.columnconfigure(2,weight=1)
        self.columnconfigure(2,pad=250)
        self.columnconfigure(3, weight=1)
        self.columnconfigure(4, weight=1)
        self.columnconfigure(5, weight=1)

        lbl1 = Label(self, text="Analyze")
        lbl1.grid(pady=4, padx=5,row=0,column=0)

        lbl2 = Label(self, text="Adjust")
        lbl2.grid(pady=4, padx=5,row=0,column=1)

        lbl3 = Label(self, text="File")
        lbl3.grid(pady=4, padx=5,row=0,column=2)

        lbl4 = Label(self, text="Integrated\nLoudness")
        lbl4.grid(pady=4, padx=5,row=0,column=3)

        lbl5 = Label(self, text="LRA")
        lbl5.grid(pady=4, padx=5,row=0,column=4)

        lbl6 = Label(self, text="Peak")
        lbl6.grid(pady=4, padx=5,row=0,column=5)

        lbl7 = Label(self, text="Progress")
        lbl7.grid(pady=4, padx=5,row=0,column=6)

        lbl8 = Label(self, text="Meets\nSpecs?")
        lbl8.grid(sticky=W, pady=4, padx=5,row=0,column=7)

        file_btn = Button(self, text="Select File",command=self.selectFile)
        file_btn.grid(row=1,rowspan=2, column=8,padx=5,pady=4)

        folder_btn = Button(self, text="Select Folder", command=self.selectFolder)
        folder_btn.grid(row=3, rowspan=2, column=8,padx=5,pady=4)

    def render(self):
        count = 0
        filebox = Listbox(self,selectmode=EXTENDED,setgrid=1)
        scrollbar = Scrollbar(filebox, orient=HORIZONTAL)
        scrollbar.config(command=filebox.xview)
        filebox.grid(row=1, column=2, rowspan=len(self.fileList), columnspan=1, sticky=N+S+E+W)
        filebox.config(xscrollcommand=scrollbar.set)
        scrollbar.pack(side=BOTTOM, fill=X)
        for file in self.fileList:
            analyze = IntVar()
            adjust = IntVar()
            Radiobutton(self, text="", variable=analyze, value=count, borderwidth=0).grid(row=count+1, column=0)
            Radiobutton(self, text="", variable=adjust,  value=count, borderwidth=0).grid(row=count+1, column=1)
            filebox.insert(END, file + "\n")
            Progressbar(self, orient=HORIZONTAL,length=100, mode='determinate').grid(row=count+1, column=6)
            count += 1

    def addToList(self, name):
        dot = re.search("\.(?=[^.]*$)",name)
        extension = name[dot.end():]
        if extension in self.allowedExtensions and not name in self.fileList:
            self.fileList.append(name)

    def selectFile(self):
        input = tkFileDialog.askopenfilename(filetypes = [('MP3', '*.mp3'), ('MP2', '*.mp2'), ('M4A', '*.m4a'), ('AIFF', '*.aiff'), ('WAV', '*.wav')], multiple = 1)
        for el in input:
            if os.path.isfile(el) and ".DS_Store" not in el:
                try:
                    self.addToList(el)
                except:
                    tkMessageBox.showerror("Some error")
        self.render()

    def selectFolder(self):
        input = tkFileDialog.askdirectory()
        for (dirpath, dirnames, filenames) in walk(input):
            for name in filenames:
                if name != ".DS_Store":
                    self.addToList(dirpath + "/" + name)
        self.render()

def main():
    root = Tk()
    app = Leveler_tk(root)
    root.mainloop()

if __name__ == "__main__":
    main()

0 个答案:

没有答案