0

我所拥有的是一个打开的窗口,它有一个列表框。这是使用一个类创建的。当我单击搜索按钮并使用不同的类找到结果时,我希望更新列表框而不必打开另一个窗口。以下是我到目前为止的代码\n

from Tkinter import *


class addTask:
    def __init__(self):
        self.addWindow = Tk()
        self.addWindow.configure(background = "black")
        self.addWindow.geometry("450x450")
        self.addWindow.resizable(width = False, height = False)
        self.addWindow.title("Add Task")

        self.addNameLabel = Label(self.addWindow,text="Add the name of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
        self.addNameLabel.place(relx=0.01, rely=0.05)
        self.nameWiget = Text (self.addWindow, width = 63, height = 1)
        self.nameWiget.place(relx=0.0, rely=0.1)

        self.addDateLabel = Label(self.addWindow,text="Add the date of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
        self.addDateLabel.place(relx=0.01, rely=0.2)
        self.dateWiget = Text (self.addWindow, width = 63, height = 1)
        self.dateWiget.place(relx=0.0, rely=0.25)

        self.addTaskLabel = Label(self.addWindow,text="Add the details of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
        self.addTaskLabel.place(relx=0.01, rely=0.35)
        self.taskWiget = Text (self.addWindow, width = 63, height = 1)
        self.taskWiget.place(relx=0.0, rely=0.4)

        addButton = Button(self.addWindow,height = 5, width = 20, text="Add Task",highlightbackground="black",font=("Helvetica",10,"bold"),command=lambda:self.saveFuntion())
        addButton.place(relx=0.25, rely=0.55)


    def saveFuntion(self):
        nameInfo = (self.nameWiget.get(1.0, END))
        dateInfo = self.dateWiget.get(1.0, END)
        taskInfo = self.taskWiget.get(1.0, END)
        print nameInfo
        task1 = Task(nameInfo,dateInfo,taskInfo)
        task1.save()
        self.nameWiget.delete(1.0,END)

class Task:
    def __init__(self,name,date,task):
        self.__name = name
        self.__date = date
        self.__task = task


    def save(self):
        fileName = open("dataFile.txt","a")
        fileName.write(self.__name)
        fileName.write(self.__date)
        fileName.write(self.__task)

class editTask:
    def __init__(self):
        self.editWindow = Tk()
        self.newWindow = Tk()
        self.newWindow.geometry("450x750")
        self.editWindow.configure(background = "black")
        self.editWindow.geometry("450x750")
        self.editWindow.resizable(width = False, height = False)
        self.editWindow.title("Edit Task")
        self.listBox = Listbox(self.editWindow,heigh = 15, width = 30)
        self.listBox.place(relx = 0.2, rely = 0.6)

        #drop down menu
        self.var = StringVar(self.editWindow)
        self.var.set("Select search critria")
        self.choices = ["Name","Date"]
        self.option = OptionMenu(self.editWindow,self.var,*self.choices)
        self.option.configure(bg = "black")
        self.option.place(relx = 0.5, rely = 0.2)

        #edit label and text box
        self.editLabel = Label(self.editWindow,text="Add the name of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
        self.editLabel.place(relx=0.01, rely=0.05)
        self.editInfoWiget = Text (self.editWindow, width = 63, height = 1)
        self.editInfoWiget.place(relx=0.0, rely=0.1)

        # search button
        searchButton = Button(self.editWindow,height = 5, width = 20, text="Search for Task",highlightbackground="black",font=("Helvetica",10,"bold"),command=lambda:self.searchFuntion())
        searchButton.place(relx=0.3, rely=0.4)

    def searchFuntion(self):

        critria = self.var.get()
        info = self.editInfoWiget.get(1.0,END)
        thing = info.split("\n")
        thing2  = thing[0]


        search = searchCritria(critria,thing2)
        search.search()


    #    def openListBox(self):






class searchCritria():

    def __init__(self,critria,info):
        self.__critria = critria
        self.__info = info

    def search(self):
        self.file = open("dataFile.txt", "r+")
        fileData = self.file.readlines()
        self.file.close()

        lengthOfFile = len(fileData)
        counter = 1
        self.name = []
        self.date = []
        self.details = []

        for i in range (lengthOfFile):
            split = fileData[i].split("\n")
            while counter == 1:
                self.name.append(split)
                break
            while counter == 2:
                self.date.append(split)
                break
            while counter == 3:
                self.details.append(split)
                break
            counter = counter +1
            if counter > 3:
                counter = 1

        if self.__critria == "Name":
            for x in range (len(self.name)):
                self.taskName = self.name[x]
                self.taskName2 = self.taskName[0]
                if self.__info == self.taskName2:
                    openWindow = True
                else :
                    openWindow = False
            if openWindow == True:
                editTask().listBox.insert(END,self.taskName2)

        if self.__critria == "Date":
            for x in range (len(self.date)):
                self.taskDate = self.date[x]
                self.taskDate2 = self.taskDate[0]
                if self.__info == self.taskDate2:
                    print "found"
                else :
                    print"not found"
class editTask2():
    def __init__(self):
        self.edit2Window = Tk()
        self.edit2Window.configure(background = "black")
        self.edit2Window.geometry("450x350")
        self.edit2Window.resizable(width = False, height = False)
        self.edit2Window.title("Edit Task")

任何帮助都会很棒

谢谢

4

1 回答 1

0

您的代码中最大的问题是您正在创建多个Tk. Tkinter 根本不是为那样工作而设计的。您应该只创建一个Tk. 如果您需要其他窗口,请创建Toplevel.

此外,您需要只调用一次mainloop此实例的函数Tk。没有它,您的 GUI 将无法工作。

如果你想更新另一个类中的列表框,而不是它创建的地方,这个概念很简单:如果 A 需要更新 B,A 需要对 B 的引用。所以,要么在创建列表框时传入对列表框的引用其他类,或者给其他类一个您可以调用的方法,您可以在创建列表框后传入对列表框的引用。

于 2013-05-23T10:53:36.283 回答