【问题标题】:Most Efficient Way of Passing Data Between UI and Data Modules in Tkinter在 Tkinter 中的 UI 和数据模块之间传递数据的最有效方式
【发布时间】:2013-09-27 04:21:14
【问题描述】:

背景:

我当前的程序基于一个 UI 模块,该模块连接到各种数据分析和生成模块。生成、分析数据并将其发送到 UI,然后可以在其中操作数据并通过分析模块放回数据,然后再刷新 UI。但是,这需要将数据从 UI 模块导入分析模块,然后将分析后的数据拉回界面,这似乎是在每次更改时创建程序的新实例——这当然不是我的意图。这是我编写的第一个依赖于我自己的几个模块同时导入的接口,早期的接口要么是自包含的,要么在模块之间共享有限的数据。

问题:

在模块之间传递信息的最有效方式是什么?避免创建“反馈”循环的最佳方法是什么(如下面的简化示例所示)?

示例:

from Tkinter import *
#Example Data_UI module
class Interface_On: 
    def Interface_Elements(self, master):
        self.master=master
        self.master.title("'Feedback' Loop")
        self.c=Canvas(self.master, width=1000, height=1000, bg='black')
        self.c.grid(row=0, rowspan=25, column=0)
        drawing_utility_run=Canvas_Draw()
        drawing_utility_run.canvas_objects(self.c)      

class Canvas_Draw:

    def canvas_objects(self, canvas):
        global new_x, new_y
        self.canvas=canvas
        new_x=[]
        new_y=[]
        from Data_Presets import a
        import Data_Generator
        Generator_run=Data_Generator.Generator()
        Generator_run.generator()       
        from Data_Generator import coordinates_x, coordinates_y
        import Data_Processor
        Process_Data=Data_Processor.Data_Processor()
        Process_Data.Process_Data()
        from Data_Processor import data_set, analysed_set, filtered_set
        for i in range(len(data_set)):
        self.canvas.create_oval(coordinates_x, coordinates_y, coordinates_x+a, coordinates_y+a, ...)

    def move_point:

        #interactive objects etc etc
        new_x.append(event.x) #etc

root=Tk()
run_it=Interface_On()
run_it.Interface_Elements(root)
root.mainloop()

#Seperate Data Analysis module
class Data_Processor:
def Process_Data(self):
    from Data_UI import new_x, new_y #This appears to create the unwanted loop
    #Data Analysis etc
            #What is the most efficient way to get data from the UI into this module and avoid creating a new instance of the UI?

【问题讨论】:

    标签: python list python-2.7 tkinter


    【解决方案1】:

    我会反过来做,让 Data_Processor 调用/实例化 GUI 类(我假设 GUI 也生成数据)。因为它知道这个类,所以它可以访问变量,所以不需要传递。在任何情况下,您都希望一个类/模块处理数据,而另一个类无需任何传递即可访问该数据,如下所示,同时从两个类打印变量。

    不需要创建类Canvas_Draw,只需在Interface_On下包含这两个函数即可。另请注意,您应该使用实例对象/变量而不是全局变量,如下例所示。类入门教程http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm

    class GUI:
        def __init__(self):
            self.x = 3
            self.y = 4
    
        def print_test_gui(self):
            print self.x, self.y
    
        def increment_vars(self):
            self.x -= 1
            self.y -= 1
    
    class Data_Processor:
        def __init__(self):
            self.x=1
            self.y=2
            self.GI= GUI()
            self.print_test()
            self.increment_vars()
            self.print_test()
            self.GI.increment_vars()
            self.print_test()
    
        def print_test(self):
            print "this x and y =", self.x, self.y
            print "GUI x and y  =", self.GI.x, self.GI.y
            self.GI.print_test_gui()
            print
    
        def increment_vars(self):
            self.x += 1
            self.y += 1
    
    DP=Data_Processor()
    

    【讨论】:

    • 感谢您的意见 :) 我的数据生成器是一个单独的模块,我的数据处理器也是如此,它们本身就是相当大的程序,我正在有效地寻找创建的最佳方法一个集线器,来自多个独立模块的所有内容都通过该集线器...
    【解决方案2】:

    您可以反转上述代码并让 GUI 调用其他代码。什么是流量?您是单击 GUI 中的按钮生成更多数据,还是只生成一组数据?

    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    class GUI:
        def __init__(self):
            root = tk.Tk()
            self.x = 3
            self.y = 4
    
            self.descr = tk.StringVar()
            tk.Label(root, textvariable=self.descr).grid()
            tk.Button(root, text='"Generate" Data', 
                      command=self.increment_vars).grid(row=1)
            self.dp = Data_Processor()
            self.increment_vars()
    
            root.mainloop()
    
        def print_test_gui(self):
            print self.x, self.y
    
        def increment_vars(self):
            self.x -= 1
            self.y -= 1
            self.dp.increment_vars()
            self.descr.set("GUI=%s, %s  Data=%s, %s" %
                          (self.x, self.y, self.dp.x, self.dp.y))
    
    class Data_Processor:
        def __init__(self):
            self.x=1
            self.y=2
            self.print_test()
    
        def print_test(self):
            print "this x and y =", self.x, self.y
    
        def increment_vars(self):
            self.x += 1
            self.y += 1
    
    G=GUI()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2018-11-10
      • 2015-06-19
      • 2020-12-03
      • 1970-01-01
      相关资源
      最近更新 更多