如何在Python的Tkinter中创建一个交互式列表,并提供可以编辑这些列表的按钮?

2024-09-30 18:13:56 发布

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

基本上我希望有一个列表,显示我存储在某个文件夹中的文件,列表旁边有按钮打开单独的窗口,可以编辑或添加新的项目到列表中。在

我希望addchar打开一个新的窗口,其中包含不同字段的空格,然后当你按下该窗口中的“创建”按钮时,它会关闭,在你刚刚输入的信息上创建一个文件(这就是我导入操作系统的原因),并在主界面的列表框中创建一个新项目作为字符的名称(这是其中一个字段)。removechar函数将删除该条目并删除该文件,editchar将打开一个类似addchar的窗口,但其中包含列表中所选项目的信息。在

编辑:这是目前为止的代码

from tkinter import *
import os
import easygui as eg

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        # character box
        Label(frame, text = "Characters Editor").grid(row = 0, column = 0, rowspan = 1, columnspan = 2)
        charbox = Listbox(frame)
        for chars in []:
            charbox.insert(END, chars)
        charbox.grid(row = 1, column = 0, rowspan = 5)
        charadd = Button(frame, text = "   Add   ", command = self.addchar).grid(row = 1, column = 1)
        charremove = Button(frame, text = "Remove", command = self.removechar).grid(row = 2, column = 1)
        charedit = Button(frame, text = "    Edit    ", command = self.editchar).grid(row = 3, column = 1)

    def addchar(self):
        print("not implemented yet")
    def removechar(self):
        print("not implemented yet")
    def editchar(self):
        print("not implemented yet")


root = Tk()
root.wm_title("IA Development Kit")
app = App(root)
root.mainloop()

Tags: 文件textimportself列表defcolumnroot
1条回答
网友
1楼 · 发布于 2024-09-30 18:13:56

好的,我为您实现了addchar和{}(并在代码中调整了一些其他内容)。我将把editchar的实现留给您——看看我为帮助您而写的,以及listbox documentation

from Tkinter import *  # AFAIK Tkinter is always capitalized
#import os
#import easygui as eg

class App:
    characterPrefix = "character_"
    def __init__(self, master):
        self.master = master  # You'll want to keep a reference to your root window
        frame = Frame(master)
        frame.pack()

        # character box
        Label(frame, text = "Characters Editor").grid(row = 0, column = 0, rowspan = 1, columnspan = 2)
        self.charbox = Listbox(frame)  # You'll want to keep this reference as an attribute of the class too.
        for chars in []:
            self.charbox.insert(END, chars)
        self.charbox.grid(row = 1, column = 0, rowspan = 5)
        charadd = Button(frame, text = "   Add   ", command = self.addchar).grid(row = 1, column = 1)
        charremove = Button(frame, text = "Remove", command = self.removechar).grid(row = 2, column = 1)
        charedit = Button(frame, text = "    Edit    ", command = self.editchar).grid(row = 3, column = 1)

    def addchar(self, initialCharacter='', initialInfo=''):
        t = Toplevel(root)  # Creates a new window
        t.title("Add character")
        characterLabel = Label(t, text="Character name:")
        characterEntry = Entry(t)
        characterEntry.insert(0, initialCharacter)
        infoLabel = Label(t, text="Info:")
        infoEntry = Entry(t)
        infoEntry.insert(0, initialInfo)
        def create():
            characterName = characterEntry.get()
            self.charbox.insert(END, characterName)
            with open(app.characterPrefix + characterName, 'w') as f:
                    f.write(infoEntry.get())
            t.destroy()
        createButton = Button(t, text="Create", command=create)
        cancelButton = Button(t, text="Cancel", command=t.destroy)

        characterLabel.grid(row=0, column=0)
        infoLabel.grid(row=0, column=1)
        characterEntry.grid(row=1, column=0)
        infoEntry.grid(row=1, column=1)
        createButton.grid(row=2, column=0)
        cancelButton.grid(row=2, column=1)

    def removechar(self):
        for index in self.charbox.curselection():
            item = self.charbox.get(int(index))
            self.charbox.delete(int(index))
            try:
                os.remove(characterPrefix + item)
            except IOError:
                print "Could not delete file", characterPrefix + item
    def editchar(self):
        # You can implement this one ;)

root = Tk()
root.wm_title("IA Development Kit")
app = App(root)
root.mainloop()

相关问题 更多 >