无法获取要为tkinter显示的列表框

时间:2011-10-24 18:49:08

标签: python tkinter

所以,我想要做的是在按下按钮并在列表框中显示内容时打开文件。这是我到目前为止,但我没有显示列表框,更不用说将信息放在列表框中了:

#!/usr/bin/perl -w

import time
from Tkinter import *
import tkFileDialog

def listbox(listbox):

    def open_file():
            file = tkFileDialog.askopenfilename()
            openFile = open(file)
            for line in openFile:
                 listbox.insert(END, line)

    open_file()


class App:

    def __init__(self, parent):

        frame = Frame(parent.title("Buttons"))
        frame.pack()
        root.pack_propagate(0)

        self.exit = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.exit.pack(side=LEFT)

        self.open = Button(frame, text="Open...", command=self.call_listbox)
        self.open.pack(side=LEFT)

        frame.listbox = Frame()
        scrollme = Scrollbar(frame.listbox)
        self.listbox = Listbox(frame.listbox, yscrollcommand = scrollme.set)
        scrollme.config(command = self.listbox.yview)
        scrollme.pack(side = RIGHT, fill = Y)
        self.listbox.pack()
        self.listbox.insert(END, "Code:")

    def call_listbox(self):
        listbox(self.listbox)

root = Tk()
app = App(root)
root.mainloop()

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:1)

您忘记打包包含列表框的框架。

FWIW,您重载名称“listbox”会使您的代码非常混乱 - 您拥有def listbox(listbox)self.listboxframe.listbox。而且你还有call_listboxListbox类来增加混乱。