【问题标题】:Passing variable to another Python Script [duplicate]将变量传递给另一个 Python 脚本 [重复]
【发布时间】:2014-02-24 04:24:36
【问题描述】:

我在将变量从一个函数传递到另一个 python 脚本中的另一个函数时遇到了困难。我已经阅读了其他答案,但他们在这个问题上并没有真正的帮助。

这是我要将变量发送到的第一个文件(为清楚起见省略了一些代码)

# TestGUI.py

from Tkinter import *
import serial
import os

class Testgui:
    def __init__(self, master):

    def writetoBOT(self,instruct):
       ser = serial.Serial(6)
       ser.baudrate = 9600
       ser.parity = serial.PARITY_NONE #set parity check: no parity
       ser.timeout = 1            #non-block read
       ser.writeTimeout = 2     #timeout for writ

       if(ser.isOpen() == False):
           ser.open()
           print ser.portstr       # check which port was really used
           ser.write(instruct)
       else :
           ser.write(instruct)

这是第二个文件:

# TestGUI_2.py

from TestGUI import Testgui

class Tracker:
    def __init__(self):
       pass
    def drive(self,cords, cords1):
       while( cords >= 320):        
           l='l'
           Testgui.writetoBOT(l)      # This is the problem line 

TypeError: unbound 方法 writetoBOT() 必须以 TestGUI 实例作为第一个参数调用(取而代之的是 str 实例)

【问题讨论】:

  • 您有 2 个文件,但您正在尝试执行 one 脚本,对吗?你的描述很混乱。

标签: python function variables arguments


【解决方案1】:

您将Testgui 声明为一个类。这要理解为骨架或线框(注意,这是捷径,而不是现实)。您需要首先从这个骨架中创建一个“真实”对象才能使用它。

testgui=Testgui(amaster)

类中可能有适用于类级别的方法(绑定函数)。这些被称为静态方法或类方法。它们必须用 python 装饰。

更多信息请参见http://en.wikipedia.org/wiki/Object_oriented_programming

【讨论】:

    【解决方案2】:

    或者,您可以为这两个脚本创建公共空间,它由数据库 - sqllite 进行

    例如,

    # file1.py
    import sqlite3
    
    con = sqlite3.connect('messages.db')
    cur = con.cursor()
    #cur.execute('CREATE TABLE message (id INTEGER PRIMARY KEY, name TEXT, content TEXT, read INTEGER)')
    #con.commit()
    
    
    for x in range(100000):
        if x in range(1, 500): 
            cur.execute('INSERT INTO message (id, name, content, read) VALUES(NULL, "Guido", "van Rossum", 0)')
            con.commit()
    
    # file2.py 
    import sqlite3
    import time
    
    con = sqlite3.connect('messages.db')
    cur = con.cursor()
    
    def listen():
        messages = cur.execute('SELECT * FROM message WHERE read=0')
        if not messages:
            return False 
        for m in messages: 
            print 'get message ', m
            cur.execute("UPDATE message SET read=1 WHERE id=?", m[0])        
            con.commit()
            print 'update db'
        return True
    
    while True: 
        listen()
        time.sleep(5)
    

    【讨论】:

      【解决方案3】:

      writetoBOT 有 2 个参数:selfinstruct。 使用Testgui 实例调用它:

      tgui=Testgui(your_master)
      tgui.writetoBOT(l)
      

      如果你想用Testgui类调用它,你仍然需要传递一个Testgui的实例:

      tgui=Testgui(your_master)
      Testgui.writetoBOT(tgui, l)
      

      【讨论】:

      • (your_master) 是调用类吗?在这种情况下是 Tracker()?
      • @user3257360 这是构建Testgui实例时需要传递的参数。见def __init__(self, master)。这段代码不是你写的吗?
      • 为了修复我的代码,我创建了另一个名为 writetoSerial() 的类。并将串行写入代码放入其中。然后导入函数然后调用它使用: from TestGUI_2 import writetoSerial 然后调用函数: l='l' t = writetoSerial() t.writetoBOT(l)
      • 感谢您的指导。我很感激
      猜你喜欢
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 2013-08-27
      • 1970-01-01
      • 2016-03-07
      • 2021-08-14
      相关资源
      最近更新 更多