如何将变量从一个类传递到另一个类python?

fnatzsnv  于 2023-01-06  发布在  Python
关注(0)|答案(1)|浏览(127)

我想将connected_complete的值从www.example.com传递main.py到tkinter_oop. py。但是,正如下面的代码所示,错误

File "C:\Users\ching\OneDrive\Desktop\final_dobot_package\pydobot\tkinter_oop.py", line 17, in __init__
    self.button_connecting_signal  =  main_dobot.dobot_automation.connected_complete()
AttributeError: type object 'dobot_automation' has no attribute 'connected_complete'

以下是通过值出现的部分:
tkinter_oop.py中的类图形

import tkinter as tk
from tkinter import ttk 
import main_dobot 

class graphic(tk.Tk , main_dobot.dobot_automation):
    def __init__(self):
        super().__init__()
        self.button_connecting_signal  =  main_dobot.dobot_automation.connected_complete
 

    def button_connected_clicked(self): 
        main_dobot.dobot_automation()
        self.connecting = tk.Label(self.tab_2,text = "N/A", font=("Inter", 25))
        self.connecting.place(x = 500  , y = 350)
        if  self.button_connecting_signal  ==  1 :
            self.connecting.config("Connected")
        elif  self.button_connecting_signal== 2 :
            self.connecting.config("Not Connected")

主_机器人.py

from serial.tools import list_ports

import pydobot
from pydobot.enums import PTPMode
from pydobot.enums import jogMode

class dobot_automation : 
    def __init__(self,connected_complete = 0 ):
        self.connected_complete = connected_complete 
        self.available_ports = list_ports.comports()
        print(f'available ports: {[x.device for x in self.available_ports]}')
        
        if len(self.available_ports) == 0:
            print('No port avaliable')
            print ('Please Check Connection')
            self.connected_complete = 2
        else:
            self.port = self.available_ports[0].device
            self.device = pydobot.Dobot(port=self.port, verbose=True) 
            self.connected_complete = 1

完整代码:
main.py

from serial.tools import list_ports

import pydobot
from pydobot.enums import PTPMode
from pydobot.enums import jogMode

class dobot_automation : 
    def __init__(self,connected_complete = 0 ):
        self.connected_complete = connected_complete 
        self.available_ports = list_ports.comports()
        print(f'available ports: {[x.device for x in self.available_ports]}')
        
        if len(self.available_ports) == 0:
            print('No port avaliable')
            print ('Please Check Connection')
            self.connected_complete = 2
        else:
            self.port = self.available_ports[0].device
            self.device = pydobot.Dobot(port=self.port, verbose=True) 
            self.connected_complete = 1
     
    def home_cabliration(self):
        if self.home_cablibration_command == 1: 
            self.device._set_queued_cmd_clear()
            self.device.set_jog_command( 1, jogMode.BP_DOWN, wait=True)
            self.device.wait(100)
            self.device.set_jog_command( 1, jogMode.IDLE , wait=True)
            self.device.wait(1000)
            self.device.set_home_params(178.9299, 3.3897 , -33.8784 , -82.7707, False)
            self.device.set_home_command (1,False)
            self.device.wait(1000)
            self.home_cablibration_command = 0 

    def cleaning_automation_process(self,automation_command):
        if automation_command == 1:
            self.device._set_queued_cmd_clear()

            #initial position 
            self.intial_moving_pos()

            #set 1 
            self.set_one_pos_up()
            self.open_grip()

            self.set_one__pos_down()
            #close grip
            self.close_grip() 
            #move up 
            self.set_one_pos_up()

            self.bath_one_pos_1_up()
            self.bath_one_pos_1_down()
            self.open_grip()
            self.bath_one_pos_1_up()
            self.close_grip() 
            self.disable_grip()

            #back to intial pos 
            self.intial_moving_pos()
            self.set_two_pos_up()
            self.open_grip()
            self.set_two_pos_down()
            #close grip
            self.close_grip() 
            #move up 
            self.set_two_pos_up()
            self.bath_one_pos_2_up()
            self.bath_one_pos_2_down()
            self.open_grip()
            self.bath_one_pos_2_up()
            self.close_grip() 
            self.disable_grip()

        
            #back to intial pos 
            self.intial_moving_pos()
            self.bath_one_pos_1_up()
            self.open_grip()
            self.bath_one_pos_1_down()
            self.close_grip() 
            self.bath_one_pos_1_up()
            self.set_one_pos_up()
            self.set_one__pos_down()
            self.open_grip()
            self.set_one_pos_up()
            self.close_grip() 

            self.intial_moving_pos()
            self.bath_one_pos_2_up()
            self.open_grip()
            self.bath_one_pos_2_down()
            self.close_grip() 
            self.bath_one_pos_2_up()
            self.set_two_pos_up()
            self.set_two_pos_down()
            self.open_grip()
            self.set_one_pos_up()
            self.close_grip() 
            (x, y, z, r, j1, j2, j3, j4) = self.device.pose()
            print(f'x:{x} y:{y} z:{z} r:{r} j1:{j1} j2:{j2} j3:{j3} j4:{j4}')
            automation_command = 0 
            

    def quit_automation_program(self) : 
        if self.quit_command == 1 :
            self.device.close()
            self.connected_complete = 2 
            self.quit_command = 0 
    
    def automation_emergency_stop(self ):
        if self.emmergency_command_stop == 1:
            self.device._set_queued_cmd_stop_exec()
            self.emmergency_command_stop = 0


    def intial_moving_pos(self): 
        self.device.move_to( 17.8941 , 6.0331 ,  0.5921 , -134.5598, PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def set_one_pos_up(self):
        self.device.move_to( 62.9159 , 13.689 , -10.0889 , 124.8797 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
  
    def set_one__pos_down(self): 
        self.device.move_to( 62.8941 , 16.9492 , 28.2911 , 124.8797 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def set_two_pos_up (self):
        self.device.move_to( 80.2371 , 6.9371 , -12.9820 , 99.4637 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def set_two_pos_down(self): 
        self.device.move_to( 80.2371 , 6.9371 , 26.238 , 99.4637 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def set_three_pos(self):
        self.device.move_to( 50.82 , -3.56 , -9.18 , -134.5598, PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
        #device.move_to( 52.29 , 31.8271 , 13.82 , -85.6, PTPMode.MOVJ_ANGLE, wait=True)
        #device.wait(1000)
    def open_grip(self):  
        self.device.grip(False)
        self.device_setupdevice.wait(1000)
        self.device.suck(False)
        self.device.wait(1000)
    def close_grip(self):    
        self.device.grip(True)
        self.device.wait(1000)
        self.device.suck(True)
        self.device.wait(1000)
    def disable_grip(self): 
        self.device.suck(False)
        self.device.wait(1000)
    def bath_one_pos_1_up(self):
        self.device.move_to( 3.1442 , 36.3588 , -14.6246 ,-68.4760  , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)    
    def bath_one_pos_1_down(self):
        self.device.move_to( 3.1442 , 36.3588 , -14.6246 , 89.4437 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
        self.device.move_to( 3.1442 , 36.3588 , 20.5754 , 89.4437 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def bath_one_pos_2_up(self):
        self.device.move_to ( 22.6588 , 46.7398 , -8.1826 , 99.4637 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
    def bath_one_pos_2_down(self):
        self.device.move_to ( 22.6588 , 46.7398 , -8.1826 , -102.1199 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)
        self.device.move_to ( 22.6588 , 46.7398 , 16.3267 , -102.1199 , PTPMode.MOVJ_ANGLE, wait=True)
        self.device.wait(1000)


def dobot_main_run(home_cablibrate_command , automation_command,emergency_stop_command,quit_command): 
    dobot_arm = dobot_automation (home_cablibrate_command , automation_command,emergency_stop_command,quit_command)
    try:
        while (1):
            dobot_arm.serial_port_connecting ()
            dobot_arm.home_cabliration()
            dobot_arm.cleaning_automation_process()
            dobot_arm.automation_emergency_stop()
            dobot_arm.quit_automation_program()
    except:
        print ("There is error in the program , please stop the program immediately ")

tkinter_循环.py

import tkinter as tk
from tkinter import ttk 
import main_dobot 

class graphic(tk.Tk , main_dobot.dobot_automation):
    def __init__(self):
        super().__init__()
       
        self.reset_clock = 0
        self.check_value = 0 
        self.done = 0
        self.minute = 1 
        self.second = 0   
        
        #button value 
        self.button_connecting_signal  =  main_dobot.dobot_automation.connected_complete
        self.button_home_cablirate_done = 0
        self.button_automation_cablirate_done = 0 
        self.button_quit_done = 0
        self.emergency_stop_done = 0   

        
        self.title('Automation Cleaning Process')
        self.geometry('1100x800+100+100')
        self.resizable(False, False)
        self.menubar = tk.Menu(self)
        filemenu = tk.Menu(self.menubar, tearoff=0)
        filemenu.add_command(label="New")
        filemenu.add_command(label="Open")
        filemenu.add_command(label="Save")
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.quit)
        self.menubar.add_cascade(label="File", menu=filemenu)
        
        helpmenu = tk.Menu(self.menubar, tearoff=0)
        helpmenu.add_command(label="Help Index")
        helpmenu.add_command(label="About...")
        self.menubar.add_cascade(label="Help",menu=helpmenu)

        #self.integer_variable_x_value = tk.IntVar()
        #self.integer_variable_y_value = tk.IntVar()
        #self.integer_variable_z_value = tk.IntVar() 
        #self.integer_variable_r_value = tk.IntVar() 
        #self.integer_variable_x_value.set(0)
        #self.integer_variable_y_value.set(0)
        #self.integer_variable_z_value.set(0)
        #self.integer_variable_r_value.set(0) 
        
        self.tabControl = ttk.Notebook(self) 
        self.tab_1 = ttk.Frame(self.tabControl)
        self.tab_2 = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tab_2,text = 'HOME')
        self.tabControl.add(self.tab_1,text = 'Option')
        self.tabControl.pack(expand = 1, fill ="both")
        
        self.set_button()
       
        self.set_text_gui()
       
        self.set_timer_counter_process(self.minute,self.second)
        #self.update_output(200,300,400,500)
        self.config(menu=self.menubar)

       





    def set_button(self):
        self.button_home_cablirate = tk.Button (self.tab_1,text = "Home Cabliration", command = self.button_home_cablirate_clicked, width = 50 , height = 5 ) 
        self.button_home_cablirate.place(x=380,y=150)
        self.button_Automation_Process = tk.Button(self.tab_1, text='Automation Process',command=self.button_automation_clicked_clock_counting_down, width = 50 , height = 5)
        self.button_Automation_Process.place(x=380,y=300)
        self.button_Stop = tk.Button(self.tab_1, text='Stop',command=self.button_quit_clicked, width = 50 , height = 5 )
        self.button_Stop.place(x=380,y=450)
        self.button_connected = tk.Button (self.tab_2,text = "Connecting Dobot" , command=self.button_connected_clicked, width=50 , height =5 )
        self.button_connected.place(x=400,y=400)
        self.emergency_button = tk.Button(self, text='Emergency Stop ',command=self.button_emergency_stop, width = 25 , height = 8 )
        self.emergency_button.place(x= 900,y = 50)
        self.slot_1 =  tk.Button(self.tab_2, text='2',command=self.two_slot_selected, width = 20 , height = 4)
        self.slot_1.place(x= 100 ,y = 250 )
        self.slot_2 =  tk.Button(self.tab_2, text='4',command=self.four_slot_selected, width = 20 , height = 4)
        self.slot_2.place(x= 500,y = 250)
        self.slot_3 =  tk.Button(self.tab_2, text='6',command=self.sixth_slot_selected, width = 20 , height = 4)
        self.slot_3.place(x= 850 ,y = 250)
        
        

    def button_connected_clicked(self): 
        main_dobot.dobot_automation()
        self.connecting = tk.Label(self.tab_2,text = "N/A", font=("Inter", 25))
        self.connecting.place(x = 500  , y = 350)
        if  self.button_connecting_signal  ==  1 :
            self.connecting.config("Connected")
        elif  self.button_connecting_signal== 2 :
            self.connecting.config("Not Connected")
            
     
     

 

    def button_home_cablirate_clicked(self): 
      print ("button home cablirate click ")
      self.button_home_cablirate_done  = 1
      
     
     
   
    def button_automation_clicked_clock_counting_down(self):
     self.done = 1 
     self.button_automation_cablirate_done = 1 
     print ("button automation click ")
     

    def button_quit_clicked(self): 
     self.button_quit_done = 1
     print ("button quit click")
     

    def button_emergency_stop (self):
        self.emergency_stop_done  = 1
        print ("button emergency clicked")

    def two_slot_selected (): 
        pass

    def four_slot_selected (): 
        pass

    def sixth_slot_selected () :
        pass

    def set_text_gui(self): 
        #declare label 
        self.choose_clean_slot = tk.Label(self.tab_2,text = "Please choose how many slot avaliable ", font=("Inter", 20))

        self.time_title = tk.Label(self.tab_2, text = "Time Process:", font=("Inter",30))
        self.time_colon= tk.Label(self.tab_2, text = ":", font=("Inter",  30))
        self.min_label= tk.Label(self.tab_2, text = "min", font=("Inter", 30))
        self.second_label=tk.Label(self.tab_2, text = "sec", font=("Inter",30))
        self.label_minute = tk.Label(self.tab_2,font=("Inter",30))
        self.label_second = tk.Label (self.tab_2,font=("Inter",30))
        self.label_minute_zero_add = tk.Label(self.tab_2,font=("Inter",30))
        self.label_second_zero_add = tk.Label(self.tab_2,font=("Inter",30))
        # position label 
        
        self.choose_clean_slot.place(x = 85  , y = 100)

        self.time_title.place(x=85,y=590) 
        self.time_colon.place(x =230,y=650)
        self.min_label.place(x=150,y=650)
        self.second_label.place(x=335,y=650)
        self.label_minute.place(x = 120 , y = 650)
        self.label_second.place(x=  278 , y = 650)
        self.label_minute_zero_add.place(x=  90 ,y=650)
        self.label_second_zero_add.place(x = 250 ,y= 650)
   
    #def update_output(self , input_dobot_x, input_dobot_y, input_dobot_z , input_dobot_r):
        # update x-axis 
        #self.integer_variable_x_value.set(input_dobot_x)
        #self.x_axis_value.config(text=str(self.integer_variable_x_value.get()))
        # update y axis 
        #self.integer_variable_y_value.set(input_dobot_y)
        #self.y_axis_value.config(text=str(self.integer_variable_y_value.get()))
        # update z axis 
        #self.integer_variable_z_value.set(input_dobot_z)
        #self.z_axis_value.config(text=str(self.integer_variable_z_value.get()))
        
        # update r axis
        #self.integer_variable_r_value.set(input_dobot_r)
        #self.r_axis_value.config(text=str(self.integer_variable_r_value.get()))
        
        #update all the variable every 100secs to GUI 
        #self.after(100,self.update)

    def set_timer_counter_process(self,minute,second):
        self.label_minute['text'] = minute
        self.label_second['text'] = second
        check_minute_length = len(str(minute))
        check_second_length = len(str(second))
        if check_minute_length == 1: 
            self.label_minute_zero_add['text'] = 0 
        else: 
            self.label_minute_zero_add['text'] =''

        if check_second_length == 1:
            self.label_second_zero_add['text'] = 0  
        else:
            self.label_second_zero_add['text'] ='' 

        if self.done == 1:      
            if second  > 0 :
                second = second - 1       
            elif second <= 0 :
                if minute > 0:
                    minute  = minute - 1  
                    second  = 60 
                else:
                    self.done = 0
        else:
            minute = 1
            second = 0           
        self.after(1000, self.set_timer_counter_process, minute , second) 

def run_GUI():
    
    User_graphic = graphic()
    User_graphic.mainloop()

run_GUI()
u2nhd7ah

u2nhd7ah1#

你好像把类和示例弄混了。
考虑这个简单的例子:

class Fruit:
    ...

class Apple(Fruit):
    ...

my_snack = Apple()

在执行了上面的步骤之后,my_snackApple的一个示例,并且通过继承也是Fruit的一个示例。FruitApple都是类(因此拼写时要大写,尽管这是一个约定而不是规则--把它当作可读代码的一个规则)。
你有这个:

class dobot_automation():
    def __init__(self):
        self.connected_complete = 'some value'

class graphic(dobot_automation):
    def __init__(self):
        self.button_connecting_signal = dobot_automation.connected_complete


my_g = graphic()

在这里,我们尝试将my_g作为graphic的一个示例(看看大写如何使其更容易理解?)然而,示例化类调用__init__,并尝试访问dobot_automation.connected_complete
在那里,dobot_automation是一个类,并且没有在类上定义类属性connected_complete,只有当类被示例化时的示例属性。
因此,尽管这可能不是您想要的,但这是可行的:

class DobotAutomation():
    def __init__(self):
        self.connected_complete = 'some value'

class Graphic(DobotAutomation):
    def __init__(self):
        super().__init__()
        self.button_connecting_signal = self.connected_complete


my_g = Graphic()

通过首先调用超类的__init__,该属性在self上可用,并且可以被引用。
您可能不希望这样做的原因是,这只是将.connected_complete的值赋给.button_connecting_signal,根据该值是否可变,您可能只是创建了一个值的额外副本(在本例中为int)。
如果只想让button_connecting_signal充当connected_complete的别名,可以执行以下操作:

class Graphic(DobotAutomation):
    def __init__(self):
        ...
        
    @property
    def button_connecting_signal(self):
        return self.connected_complete  # after all, this is a DobotAutomation as well

为了完整起见,class属性定义如下:

class Fruit:
    typical_calories = 90

class Apple(Fruit):
    def __init__(self):
        self.calories = self.typical_calories + 5

my_snack = Apple()
print(my_snack.calories)  # prints 95

注意,这也适用于Apple的构造函数:

self.calories = Fruit.typical_calories + 5

但是这直接从类Fruit中获取.typical_calories *,而不是从Apple的祖先中获取(这可能会重新定义该值)。

相关问题