如何在Python的Tkinter中制作一个带有可编辑这些列表的按钮的交互式列表? [英] How can I make a in interactive list in Python's Tkinter complete with buttons that can edit those listings?

查看:1043
本文介绍了如何在Python的Tkinter中制作一个带有可编辑这些列表的按钮的交互式列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Basically I want there to be a list, which displays files I have stored in a certain folder, and beside the list there are buttons which open up separate windows which can edit or add a new item to that list.

我希望addchar打开一个新窗口,其中包含用于不同字段的空格,然后当您在该窗口中按创建"按钮时,它将关闭,并在您刚刚输入的信息上创建一个文件(这就是为什么我导入了os ),并在主界面的列表框中创建新项作为字符名称(这是字段之一). removechar函数将删除该条目并删除文件,editchar将打开一个类似于addchar的窗口,但列表中包含所选项目的信息.

I want addchar to open up an new window with spaces for different fields, then when you press a "create" button in that window it closes, creates a file on the info you just entered (that's why i've imported os) as well as creating a new item on the main interface's listbox as the name of the character (which is one of the fields). The removechar function would remove that entry and delete the file, and editchar would open up a window similar to addchar, but with the info of the selected item on the list.

这是到目前为止的代码

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的实现留给您-看一下我为您提供的帮助以及

Ok, I implemented addchar and removechar for you (and tweaked a few other things in your code). I'll leave the implementation of editchar to you - take a look at what I wrote to help you, as well as the 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()

这篇关于如何在Python的Tkinter中制作一个带有可编辑这些列表的按钮的交互式列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆