簡體   English   中英

如何在Python的Tkinter中制作一個帶有可編輯這些列表的按鈕的交互式列表?

[英]How can I make a in interactive list in Python's Tkinter complete with buttons that can edit those listings?

基本上,我希望有一個列表,該列表顯示我存儲在某個文件夾中的文件,並且在列表旁邊有一些按鈕,可打開單獨的窗口,這些窗口可以編輯或向該列表添加新項目。

我希望addchar打開一個新窗口,其中包含用於不同字段的空格,然后當您在該窗口中按“創建”按鈕時,它會關閉,並在您剛剛輸入的信息上創建一個文件(這就是為什么我導入了OS)就像在主界面的列表框中創建一個新項目一樣,將其作為字符的名稱(這是字段之一)。 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()

好的,我為您實現了addcharremovechar (並在代碼中進行了一些其他調整)。 我將把editchar的實現editchar您-看一下我為您提供的幫助以及列表框文檔

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM