无法显示tkinter的列表框

0 投票
1 回答
545 浏览
提问于 2025-04-17 04:55

我想做的是,当我按下一个按钮时打开一个文件,并把文件里的内容显示在一个列表框里。现在我有了这些代码,但列表框没有显示出来,更别提里面的内容了:

#!/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 个回答

1

你忘记把包含列表框的框架打包了。

顺便说一下,你把“列表框”这个名字用得太多了,搞得代码很混乱。你有 def listbox(listbox),还有 self.listboxframe.listbox。另外,你还有 call_listboxListbox 类,这些都让事情变得更加复杂。

撰写回答