如何将函数结果调用到另一个函数中的tkinter窗口列表框中?

2024-09-29 21:30:13 发布

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

有人能帮我完成这个代码吗?我试图将函数的结果传递给tkinter框架内的父函数,在本例中,它是一个名为“processFile”的嵌套函数。我最近才开始使用类,所以我还有很多东西要学,我相信你很快就会指出,到目前为止,我所做的不是最佳实践,也不是有效的

我尝试将processFile设置为类中的常规函数,以及下面代码中所示的嵌套函数。我遇到的大多数错误是

第24行,在 button1=按钮(ouUpdate,text='Open File',command=lambda:processFile(self,filename)) 名称错误:未定义名称“filename”

我理解为什么,我意识到使用列表框我必须首先将结果字符串转换为列表,但我需要帮助来实现这一点

注意:我最终会将csv读取器写入processFile函数,以将csv结果读入列表框,但我现在只想开始

from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfilename

class GUI:
    def mainPage(self):
        home = Frame(root)
        home.place(relwidth=1, relheigh=1)
        label = Label(home, text='Cyber Database Console', bg='#ccffcc', font=('Arial', 18))
        label.place(relx=0 , rely=0, relheigh=0.1, relwidth=1)
        button1 = Button(home, text='OU Update', command=lambda: self.ouUpdate())
        button1.place(relx=0.1, rely=0.2, relheigh=0.05, relwidth=0.2)
        button2 = Button(home, text='VID Update', command=lambda: self.vIDUpdate())
        button2.place(relx=0.1, rely=0.3, relheigh=0.05, relwidth=0.2)


    def ouUpdate(self):
        ouUpdate = Frame(root)
        ouUpdate.place(relwidth=1, relheigh=1)
        label = Label(ouUpdate, text='Database OU Update Console', bg='#ccffcc', font=('Arial', 18))
        label.place(relx=0 , rely=0, relheigh=0.1, relwidth=1)
        button1 = Button(ouUpdate, text='Back', command=lambda: self.mainPage())
        button1.place(relx=0, rely=0.1, relheigh=0.05, relwidth=0.2)
        button1 = Button(ouUpdate, text='Open File', command=lambda: processFile(self, filename))
        button1.place(relx=0, rely=0.4, relheigh=0.05, relwidth=0.2)
        listing = Listbox(ouUpdate)
        listing.place(relx=0.1, rely=0.6, relheigh=0.1, relwidth=0.5)

        def processFile(self, filename):
            filename = askopenfilename()
            return filename

    def vIDUpdate(self):
        vIDUpdate = Frame(root)
        vIDUpdate.place(relwidth=1, relheigh=1)
        label = Label(vIDUpdate, text='Database V-ID Console', bg='#ccffcc', font=('Arial', 18))
        label.place(relx=0 , rely=0, relheigh=0.1, relwidth=1)
        button1 = Button(vIDUpdate, text='Back', command=lambda: self.mainPage())
        button1.place(relx=0, rely=0.1, relheigh=0.05, relwidth=0.2)


root = Tk()
canvas = Canvas(root, heigh=500, width=600)
canvas.pack()

begin = GUI()

begin.mainPage()

root.mainloop()

Tags: lambda函数textselfplacerootfilenamecommand
1条回答
网友
1楼 · 发布于 2024-09-29 21:30:13

processFile(self, filename):中不需要filename就可以从函数中获取值

 def processFile(self):
     return askopenfilename()

 ... command=lambda:processFile(self)

只有当您想将文件名发送到函数时才需要它

但是Button不能得到这个结果并将其赋值给变量,所以最好在函数内部执行

 def processFile(self):
     self.filename = askopenfilename()

然后您可以在类中的其他方法中使用此变量

但您不能越过它返回到ouUpdate,因为这个函数是在开始时执行并完成的,甚至在您看到窗口之前。您应该在这个函数中直接使用filename

 def processFile(self):
     self.filename = askopenfilename()
     data = open(self.filename).read()
     # ... process data ...

代替command=lambda: self.mainPage()你可以做command=self.mainPage
(没有lambda()

其他的也一样

command=lambda: self.ouUpdate()->command=self.ouUpdate

command=lambda: self.vIDUpdate()->command=self.vIDUpdate

如果将processFile(self)作为类中的普通方法,而不是嵌套的,那么也可以使用command=self.processFile

相关问题 更多 >

    热门问题