在python中使用pubsub和传递定义时出现的问题
在childframe类中从pubsub订阅消息时出现问题。我不知道如何编写正确的代码来显示从大型机中选择的内容。我也无法在childframe类中传递定义行。我不知道我是如何指定我的问题的,我知道这很难理解,但在这段代码中,我想你会找到我想要的东西。我是python新手,所以plizz不要批评我在python中使用pubsub和传递定义时出现的问题,python,wxpython,publish-subscribe,pyodbc,Python,Wxpython,Publish Subscribe,Pyodbc,在childframe类中从pubsub订阅消息时出现问题。我不知道如何编写正确的代码来显示从大型机中选择的内容。我也无法在childframe类中传递定义行。我不知道我是如何指定我的问题的,我知道这很难理解,但在这段代码中,我想你会找到我想要的东西。我是python新手,所以plizz不要批评我 import wx from wx.lib.pubsub import Publisher as pub import pyodbc cnxn = pyodbc.connect("DRIVER={SQ
import wx
from wx.lib.pubsub import Publisher as pub
import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server Native Client 11.0};SERVER=10.75.79.215;DATABASE=HUB_DATA;UID=sa;PWD=password")
cursor = cnxn.cursor()
class Mainframe(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.quote1 = wx.StaticText(self.panel, label = "id")
self.test=wx.StaticText(self.panel, label = "")
self.editquote1 = wx.TextCtrl(self.panel, size = (140, -1))
self.button = wx.Button(self.panel, label = "search")
#
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, wx.ID_ANY, wx.ALL | wx.EXPAND)
#
self.sizer = wx.GridBagSizer(6, 6)
self.sizer.Add(self.quote1, (0, 0))
self.sizer.Add(self.editquote1, (0, 1))
self.sizer.Add(self.button, (3, 1))
self.sizer.Add(self.test, (4, 1))
#
self.border = wx.BoxSizer()
self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 6)
#
self.panel.SetSizerAndFit(self.border)
self.SetSizerAndFit(self.windowSizer)
#
self.button.Bind(wx.EVT_BUTTON, self.Onbutton and self.afterOnbutton)
def Onbutton(self,e):
var = self.editquote1.GetValue()
if len(var)==9:
self.test.SetLabel('True')
if len(var)==9:
cursor.execute("""SELECT COUNT(DPVLDT) as amount
FROM [HBGE_Reports].[dbo].[hub_DDJPFile] s
where exists (SELECT [ZGDCS]
FROM [HUB_DATA].[dbo].[SSCUSTP] d
where [ZGIDNO]=?
and
s.DPACS=d.ZGDCS)""",str(var))
raw = cursor.fetchone()
a = raw.amount
pub.sendMessage("amount", a)
cursor.execute("""declare @a varchar(20)
set @a=?
SELECT [DPVLDT]
,[DPCPDT]
,[DPACB]
,[DPACS]
,[DPACX]
,[DPCYCD]
,[DPDLCD]
,[RCY_AMOUNT]
,[LCY_AMOUNT]
,[DPBLBL]
,[DPNAR1]
,[DPNAR2]
,[DPNAR3]
,[DPNAR4]
FROM [HBGE_Reports].[dbo].[hub_DDJPFile] s
where exists (SELECT [ZGDCS]
FROM [HUB_DATA].[dbo].[SSCUSTP] d
where [ZGIDNO]=@a
and
s.DPACS=d.ZGDCS)
order by [DPVLDT] desc""", str(var))
rows = cursor.fetchall()
for i in range(a):
pub.sendMessage(i, rows[i].DPVLDT)
else:
self.test.SetLabel('False')
def afterOnbutton(self,e):
wx.CallAfter(self.Onbutton,e)
var = self.editquote1.GetValue()
if len(var)==9:
self.new = Childframe(None)
self.new.Show()
class Childframe(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel=wx.Panel(self)
self.windowSizer = wx.BoxSizer()
self.windowSizer.Add(self.panel, wx.ID_ANY, 13, wx.ALL | wx.EXPAND, 13)
pub.subscribe(self.amount, "amount")
for n in range(3):
pub.subscribe(self.row, n)
def amount(self, amount):
for n in range(amount.data):
[].append(wx.StaticText(self.panel, id=n, label='test', pos=(20, 30 * n)))
N = amount.data
return N
#I'm having problem here. I can't get N and I don't know how to SetLabel in the static text above
def row(self, row):
for n in range(N):
[].SetLabel(str(row.data))
if __name__=='__main__':
app=wx.App(False)
frame1=Childframe(None)
frame=Mainframe(None)
frame.Show()
app.MainLoop()
问题是子帧创建和消息发送之间的时间错误。当大型机发送消息时,新帧还不存在,因此任何人都不会收到消息。尝试拆分绑定到按钮的事件,而不是:
self.button.Bind(wx.EVT_BUTTON, self.Onbutton and self.afterOnbutton)
尝试:
并修改afterOnbutton中的代码,以便在以下情况下调用另一个函数Onbutton:
def afterOnbutton(self):
wx.CallAfter(self.Onbutton)
var = self.editquote1.GetValue()
if len(var)==9:
self.new = Childframe(None)
self.new.Show()
def Onbutton(self):
var = self.editquote1.GetValue()
if len(var)==9:
self.test.SetLabel('True')
... same ...
好的,我会尝试,但收到消息。问题是我不能把第二条消息放在函数中,更新第一次按摩函数的创建。谢谢你的回复,我不太明白。您可以继续发送具有相同主题的不同消息(例如,在本例中,“金额”是一个主题),订阅该主题的对象将继续接收新消息。是的,将接收对象,正如您在我的示例中看到的,def amount将创建静态文本,其中包含我发送给它的金额。问题是,在创建静态文本列表时,我无法更改其标签。您是否尝试将处理程序分配给静态文本,然后使用handler.SetValue(“字符串值”)设置其值?
def afterOnbutton(self):
wx.CallAfter(self.Onbutton)
var = self.editquote1.GetValue()
if len(var)==9:
self.new = Childframe(None)
self.new.Show()
def Onbutton(self):
var = self.editquote1.GetValue()
if len(var)==9:
self.test.SetLabel('True')
... same ...