what i have is a window that opens up and it has a list box. this is created using one class. when i click the search button and results are found using a different class, i want the list box to update without having to open up another window. below is my code so far\n

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

any help would be great

任何帮助都会很棒

thanks

1 个解决方案

#1


0

The biggest problem in your code is that you're creating multiple instances of Tk. Tkinter simply isn't designed to work that way. You should only ever create a single instance of Tk. If you need additional windows, create instances of Toplevel.

您的代码中最大的问题是您正在创建多个Tk实例。 Tkinter根本就不是那种工作方式。您应该只创建一个Tk实例。如果您需要其他窗口,请创建Toplevel的实例。

Also, you need to call the mainloop function of this instance of Tk exactly once. Without it, your GUI will not work.

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

If you want to update a listbox in another class than where it was created, the concept is pretty simple: if A needs to update B, A needs a reference to B. So, either pass in a reference to the listbox when you create the other class, or give the other class a method you can call where you pass in the reference to the listbox after it was created.

如果你想更新另一个类中的列表框而不是它创建的列表框,那么这个概念非常简单:如果A需要更新B,A需要对B的引用。因此,在创建时要么传入对列表框的引用其他类,或者为其他类提供一个方法,您可以在创建它之后传入对列表框的引用。

更多相关文章

  1. python打印列表的下标和值的例子:
  2. 使用python如何在列表列表中找到元素,而关键元素是内部列表的元素
  3. Python学习记录--关于列表和字典的比较
  4. 如何将两个列表中的数据写入csv中的列?
  5. 在python 3.3列表中查找最小值
  6. python基础练习--列表问题
  7. 如何用所有可能的方式将一个列表分割成对?
  8. 学习python的第十六天(迭代器,三元表达式,列表生成式,字典生成式,
  9. Ansible:维护sudoers列表的最佳实践

随机推荐

  1. 从零开始学习Android开发
  2. [Android(安卓)API学习]Data Storage胡乱
  3. 怎么让Linearlayout里面的textview垂直居
  4. 《疯狂Android讲义》第二版目录
  5. Android内存泄漏检测工具大全
  6. Android(安卓)OCR之tesseract
  7. Android TextView的一些小知识
  8. 消息驱动 Looper类
  9. android的Theme
  10. RadioGroup和RadioButton