【问题标题】:wxpython: How do I update my panels text with SetLabel from within an imported script?wxpython:如何从导入的脚本中使用 SetLabel 更新面板文本?
【发布时间】:2023-03-25 12:58:01
【问题描述】:

我有一个带有一些按钮的 wxpython 面板。其中一个按钮执行控制实验室中的一台设备的 python 脚本。在python脚本中有一个while循环,看起来像,

 ave_number = 5000
 gpibObj.command('FAVN2,'+str(ave_number)) # Number of Averaging
 gpibObj.command('STRT') #Start measurement bData
 time.sleep(0.5) 
 avg = 0
 print "Number of averages to be taken:",ave_number
 while avg < ave_number:
      avg=int(gpibObj.query("NAVG?0"))
      print('Averages done:'+str(avg))
      sys.stdout.write("\033[F")
      time.sleep(0.3)

目前这打印 this 只是告诉脚本暂停,直到平均数达到它应该在的位置,并将当前平均数打印到命令行。我的问题是,有没有一种方法可以将面板当前设置为将变量 avg(在 while 循环中的变量、面板代码中的导入脚本中的变量)传递给类似的命令,

self.histstatusTXT.SetLabel(temptxt)

这样我就可以在面板上看到它实时更新了。

为了清楚起见,按钮看起来像:

self.btn = wx.Button(self.panel,wx.ID_ANY,"Collect SR785 FFT Only",(5,45))
self.btn.Bind(wx.EVT_BUTTON,self.runScript,self.btn)

它正在执行的函数在哪里

def runScript(self, e):
    dlg = wx.TextEntryDialog(self.panel,'New Directory Name?',"path-save","",style=wx.OK)
    dlg.ShowModal()
    dir = dlg.GetValue()
    dlg.Destroy()
    os.system("mkdir {0}".format(dir))
    dlg = wx.TextEntryDialog(self.panel,'Name of Files?',"path-save","",style=wx.OK)
    dlg.ShowModal()
    filename =dlg.GetValue()
    dlg.Destroy()
    x = dir + '/' + filename
    self.sb.SetStatusText('Working')
    temptxt = "Data saved to {0}".format(x)
    self.histstatusTXT.SetLabel("Collecting SR785 Data")
    wx.Yield()
    fft_group_SINGLE_FFT_function.FFT1(x)
    self.sb.SetStatusText('Ready')
    self.histstatusTXT.SetLabel(temptxt)

并且函数 fft_group_SINGLE_FFT_function.FFT1(x) 包含上面的 while 循环。如果问题没有意义,请道歉。如果它没有让我知道,我可以尝试用不同的方式解释。

谢谢。

【问题讨论】:

  • wx.lib.neweventpubsub 都允许以事件驱动的方式在子进程和主进程之间进行通信。

标签: wxpython panel


【解决方案1】:

您可以尝试以下方法:

将标签本身 (self.histstatusTXT) 或指向 (self.histstatusTXT.SetLabel) 的函数指针传递给 fft_group_SINGLE_FFT_function.FFT1() 作为附加参数(如果此函数在其他地方使用,则可能是可选的)参数。 这允许您在 FFT1 函数中设置标签文本。

我将传递函数指针,因此FFT1 函数将无法更改hisistatusTXT 标签的其他属性(除非您需要)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多