python传递类的实例_使⽤Python将变量从⼀个类实例传递到
另⼀个类实例?
我在将⼀个类实例中定义的变量传递到另⼀个类实例时遇到问题。我对使⽤类⽐较陌⽣,但据我所知,变量可以通过简单地定义为类实例的⼀部分(例如在下⾯的⽰例中)从⼀个实例传递到另⼀个实例。虽然我以前使⽤过这个模型,但我从未尝试过使⽤像wxPython这样的GUI框架来实现这⼀点。class Foo(object):
def __init__(self, var):
self.var = var
class Bar(object):
def do_something(self, var):
print var*3
if __name__ == '__main__':
f = Foo(3)
b = Bar()
b.do_something(f.var)
我遇到的问题是wxPython实例似乎是预定义的,不接受任何附加参数(允许我只向类实例传递标题、⼤⼩等内容)。
我⾯临的另⼀个问题是,我试图通过调⽤⼀个对话框窗⼝来深⼊传递变量三个类,并从对话框中调⽤⼀个单独的类来启动⼀个⼯作线程。
所以我的问题是:如何将变量从⼀级实例传递到三级实例?
如何重写wxPython实例以允许定义其他变量?
或者,是否可以创建⾃定义事件处理程序来传递必要的数据?
澄清。。。
我正在使⽤Python,我想我理解使⽤类和带有Tkinter和wxPython(在这个项⽬中使⽤)框架的GUI编程
的基本原理。我已经编写了⼀个从⽤户那⾥获取⼀些数据的主类/实例,我希望能够传递self.main_instance_var中存储的信息,并将其传递给第⼆个类/实例(在本例中,是从第⼀个类调⽤的进度对话框窗⼝)。
当我试图在进度对话框中使⽤上述模型时,出现了⼀个⾮常不格式化的语法错误(“关键字arg之后是⾮关键字arg”)。阻⽌我进⼀步将变量从“进度”对话框窗⼝传递到⼯作线程。如果我得到⼀个异常,那将是⼀件事,但语法错误我不明⽩。下⾯是⼀个简短的例⼦:class ProgressDialog(wx.Dialog):
def __init__(self, parent, title, myVar): # Generates syntax error on this line
super(ProgressDialog, self).__init__(parent=parent,
title=title, size=(500, 110))
self.var = myVar
基本来源(按要求,很抱歉这么脏):import time
import os, sys, wx
from ftplib import FTP_TLS
from threading import Thread
from wx.lib.pubsub import Publisher
>>>>>>>>>>>>>>##
class FtpValues(object):
""" Returns a property attribute - called by FtpFileTransfer
Used to set values/variables for Host, USERID, PASSWD, FILE """
#----------------------------------------------------------------------
def __init__(self):
self.varList = None
#----------------------------------------------------------------------
python新手代码useriddef GetValues(self):
return self.varList
#----------------------------------------------------------------------
def SetValues(self, HOST, USERID, PASSWD, FILE):
self.varList = [HOST, USERID, PASSWD, FILE]
#----------------------------------------------------------------------
def DelValues(self):
del self.valList
Values = property(GetValues, SetValues, DelValues, "Set/Get FtpValues")
>>>>>>>>>>>>>>## class FtpFileTransfer(Thread):
"""Test Worker Thread Class."""
#----------------------------------------------------------------------
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.StartTransfer() # start the thread
#----------------------------------------------------------------------
def StartTransfer(self): # was named run - started automatically
"""Run Worker Thread.""" # when called by the start method
# This is the code executing in the new thread.
HOST, USERID, PASSWD, FILE = FtpValues.Values
BLOCKSIZE = 57344
try:
ftp = FTP_TLS(HOST)
ftp.login(USERID, PASSWD)
ftp.prot_p()
ftp.voidcmd("TYPE I")
f = open(FILE, 'rb')
datasock, esize = ansfercmd(
'STOR %s' % os.path.basename(FILE))
size = os.stat(FILE)[6]
bytes_so_far = 0
while 1:
buf = f.read(BLOCKSIZE)
if not buf:
break
datasock.sendall(buf)
bytes_so_far += len(buf)
msg = [bytes_so_far, size]
Publisher().sendMessage("update", msg)
except: raise
finally:
try:
datasock.close()
f.close()
ftp.voidresp()
ftp.quit()
print ''
except: pass
wx.CallAfter(Publisher().sendMessage, "update", "Database Transfer Complete!")
>>>>>>>>>>>>>>## class ProgressDialog(wx.Dialog):
def __init__(self, parent, title):
super(ProgressDialog, self).__init__(parent=parent,
title=title, size=(500, 110))
self.displayLbl = wx.StaticText(self, -1, 'Verifying ', (20, 20)) #Preparing self.gauge = wx.Gauge(self, -1, 100, (20, 45), (370, 24))
self.btn = btn = wx.Button(self, -1, 'Cancel', (400, 45), (-1, 25))
btn.Bind(wx.EVT_BUTTON, self.OnClose)
# listens for response from worker thread
Publisher().subscribe(self.updateDisplay, "update")
FtpFileTransfer()#.StartTransfer(HOST, USERID, PASSWD, FILE) #Start the FTP Worker Thread
#self.OnStart()
#----------------------------------------------------------------------
def run(self):
FtpFileTransfer(HOST, USERID, PASSWD, FILE)
#----------------------------------------------------------------------
def OnClose(self, event):
""" Place Holder """
if self.btn.GetLabel() == 'Finish':
# Do Something!
pass
return None
#----------------------------------------------------------------------
def updateDisplay(self, msg):
""" Receives data from thread and updates the display """
if isinstance(msg.data, list):
bytes_so_far, size = msg.data
k = 100 * bytes_so_far / size
self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size)) self.gauge.SetValue(k)
else:
self.displayLbl.SetLabel("%s" % msg.data)
#self.btn.Enable()
self.btn.SetLabel('Finish')
>>>>>>>>>>>>>>## class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here")
self.btn = btn = wx.Button(panel, label="Start Thread")
self.gauge = wx.Gauge(panel, -1, 100, size=(370, 24))
btn.Bind(wx.EVT_BUTTON, Button)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(self.gauge, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
self.VarData()
# create a pubsub receiver
Publisher().subscribe(self.updateDisplay, "update")
#----------------------------------------------------------------------
def onButton(self, event):
"""
Runs the thread
"""
chgdep = ProgressDialog(None, title='File Transfer. . .')
chgdep.ShowModal()
#chgdep.Destroy()
#----------------------------------------------------------------------
def updateDisplay(self, msg):
"""
Receives data from thread and updates the display
"""
if isinstance(msg.data, list):
bytes_so_far, size = msg.data
k = 100 * bytes_so_far / size
self.displayLbl.SetLabel("Sent %d of %d bytes %.1f%%" % (bytes_so_far, size, 100 * bytes_so_far / size)) self.gauge.SetValue(k)
else:
self.displayLbl.SetLabel("%s" % msg.data)
self.btn.Enable()
#----------------------------------------------------------------------
def VarData(self):
HOST = '127.0.0.1'
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
推荐文章
热门文章
-
随机森林算法的改进方法
2024-10-02 -
基于随机森林算法的风险预警模型研究
2024-10-02 -
Python中的随机森林算法详解
2024-10-02 -
随机森林发展历史
2024-10-02 -
如何使用随机森林进行时间序列数据模式识别(八)
2024-10-02 -
随机森林回归模型原理
2024-10-02 -
如何使用随机森林进行时间序列数据模式识别(六)
2024-10-02 -
如何使用随机森林进行时间序列数据预测(四)
2024-10-02 -
如何使用随机森林进行异常检测(六)
2024-10-02 -
随机森林算法和grandientboosting算法 -回复
2024-10-02 -
随机森林方法总结全面
2024-10-02 -
随机森林算法原理和步骤
2024-10-02 -
随机森林的原理
2024-10-02 -
随机森林 重要性
2024-10-02 -
随机森林算法
2024-10-02 -
机器学习中随机森林的原理
2024-10-02 -
随机森林算法原理
2024-10-02 -
使用计算机视觉技术进行动物识别的技巧
2024-10-02 -
基于crf命名实体识别实验总结
2024-10-02 -
transformer预测模型训练方法
2024-10-02
最新文章
-
随机森林算法介绍及R语言实现
2024-10-02 -
基于随机森林优化的神经网络算法在冬小麦产量预测中的应用研究_百度文 ...
2024-10-02 -
基于正则化贪心森林算法的情感分析方法研究
2024-10-02 -
随机森林算法和grandientboosting算法
2024-10-02 -
基于随机森林的图像分类算法研究
2024-10-02 -
随机森林结合直接正交信号校正的模型传递方法
2024-10-02
发表评论