将一个对象从一个类传递给另一个使用它的main()函数调用的类

时间:2018-01-15 16:33:00

标签: python class object tkinter

我的程序以“Loading_UI”类开头,该类显示一个简单的加载栏,并创建一个包含从.csv文件中提取的数据的对象。

当加载(=数据获取)完成时,加载栏会自行销毁(root.destroy()),然后我想将我创建的对象传递给另一个将显示程序主UI的类。为了保持整洁,我想从自己的.py文件中调用主UI并拥有Main_UI类。

使用上述想法,我的问题是:如何将对象从Loading_UI类传递到Main_UI类?

(从RUN_loading.py运行程序)

DataBase.py)简单对象创建:

class DataBase:

    def __init__(self, name):
        self.name = name

    def show_content(self):
        print(self.name)

在另一个类Loading_UI的.py文件中,如下所示:

带有加载栏模拟的

RUN_Loading.py)用户界面:

from tkinter import *
from DataBase import DataBase

class Loading_UI:

    def __init__(self, master):

        self.master = master
        self.DataBase = DataBase("This is our object.")
        self.load_pct = DoubleVar()
        self.load_pct.set(0)
        loading_bar = Label(textvariable = self.load_pct)
        loading_bar.grid()

        self.handle_loading_progress()

    def handle_loading_progress(self):

        if self.load_pct.get() < 10:
            self.load_pct.set(self.load_pct.get() + 1)
            self.master.after(200, self.handle_loading_progress)
        else:
            self.show_main()

    def show_main(self):

        root.destroy()
        from UI_Main import Main_UI
        Main = Main_UI(self.DataBase)
        Main.main()

root = Tk()
root.geometry("100x100+300+300")
app = Loading_UI(root)
root.mainloop()

需要处理对象的主UI看起来像这样。

(UI_Main.py)加载完成后弹出的主UI窗口。

from tkinter import *

class Main_UI:

    def __init__(self, database_object):

        self.DataBase = database_object

        # Run this function to check that the 
        # data object is properly received.
        self.DataBase.show_content()

root = Tk()
app = Main_UI(root, database_object)
root.mainloop()

if __name__ == '__main__':
    main()

以上显然是错误的,但我只是想提出我的想法。运行上面的代码给出:

  

NameError:未定义名称“database_object”

编辑:用实际(简化)示例编辑代码。

3 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法可能是在Loading_UI文件中创建Main_UI对象。

Loading_UI.py:

import Main_UI

# Create your progress bar
data = getDataFromCSV('file.csv')
# Delete your progress bar

main_ui = Main_Ui(data)

答案 1 :(得分:0)

我不是tkinter的专家,但我已经对它的文档进行了一些研究,这就是我的工作方式。

Tkinter(就像pygtk一样)具有触发它们的事件和信号的概念。您可以使用bind函数bind任何tkinter小部件(例如进度条)中的事件,并具有将对此进行操作的回调函数。显然,有一个预定义的tkinter events列表可供使用,在您的情况下,我认为<Destroy> A widget is being destroyed.将是有用的。

所以在loading_ui.py文件中:

widget = ttk.Progressbar(parent, option=value, ...) # create your progressbar

#Bind it to the destroy event. Once destroyed call the proceed_with_main function and pass your object
widget.bind('<Destroy> ', lambda event, db=self.DataBase:
                         self.proceed_with_main(db)) 

def proceed_with_main(db): 
    from UI_Main import Main_UI

    Main = Main_UI(db)
    Main.main()

答案 2 :(得分:0)

好的,有很多事情都没有在这里完成。更正将直接发布在代码中。

(Run_loading.py)

from tkinter import *
from DataBase import DataBase

class Loading_UI:

    def __init__(self, master):

        self.master = master
        self.DataBase = DataBase("This is our object.")
        self.load_pct = DoubleVar()
        self.load_pct.set(0)
        loading_bar = Label(textvariable = self.load_pct)
        loading_bar.grid()

        self.handle_loading_progress()

    def handle_loading_progress(self):

        if self.load_pct.get() < 10:
            self.load_pct.set(self.load_pct.get() + 1)
            self.master.after(200, self.handle_loading_progress)
        else:
            self.show_main()

    def show_main(self):

        root.destroy()
        from UI_Main import Main_UI
        Main = Main_UI(self.DataBase)
        # Main.main() This line is wrong, your object Main_UI() does not have a defined 'main()' function

root = Tk()
root.geometry("100x100+300+300")
app = Loading_UI(root)
root.mainloop()

(UI_Main.py)

from tkinter import *

class Main_UI:

    def __init__(self, database_object):

        self.DataBase = database_object

        # Run this function to check that the 
        # data object is properly received.
        self.DataBase.show_content()
        self.tkWindow = Tk() # if you want to keep an new window you could keep this
        self.tkWindow.mainloop()

""" You shouldn't do things outside of the __init__() function of this class.
root = Tk()  lines to delete
app = Main_UI(root, database_object) lines to delete
root.mainloop() lines to delete """

""" This part is not necessary 
if __name__ == '__main__': 
    main() """