是否可以为ComboBox和RadioBox事件写入一个def?

2024-09-30 14:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,代码写入用户选择的字符串的一部分。在每个事件之后,TextCtrl将获得一个字符串,直到用户将文本复制到剪贴板为止。它工作得很好,但并不优雅,因为代码中有太多的重复

import wx   
import pyperclip

class Ementor(wx.Frame): 

def __init__(self, parent, title): 
  super(Ementor, self).__init__(parent, title = title,size = (500,300)) 

  self.InitUI() 

def InitUI(self):    
  pnl = wx.Panel(self)
  menubar=wx.MenuBar()
  filem=wx.Menu()
  clearmenuitem = filem.Append(wx.NewId(), "Clea&r","Clear TextBox")
  self.Bind(wx.EVT_MENU, self.onClear, clearmenuitem)
  exitmenuitem = filem.Append(wx.NewId(), "Exi&t", "Bye bye")
  self.Bind(wx.EVT_MENU, self.onExit, exitmenuitem)
  editm=wx.Menu()
  copymenuitem = editm.Append(wx.NewId(), "&Copy", "Copy String")
  self.Bind(wx.EVT_MENU, self.onCopy, copymenuitem)
  helpm=wx.Menu()
  helpmenuitem = helpm.Append(wx.NewId(), "&About","Details")
  self.Bind(wx.EVT_MENU, self.onHelp, helpmenuitem)

  menubar.Append(filem, '&File')
  menubar.Append(editm, '&Edit')
  menubar.Append(helpm, '&Help')

  self.SetMenuBar(menubar)

  # Campos de preenchimento da ementa
  lblList = ['Processo Administrativo Sancionador. ','Processo Administrativo Contencioso. ','Processo Administrativo Simplificado. ']
  self.rbox = wx.RadioBox(pnl, label = 'Tipo', pos = (10,10), choices = lblList, majorDimension = 3, style = wx.RA_SPECIFY_ROWS) 
  self.rbox.Bind(wx.EVT_RADIOBOX,self.onRadioBox) 

  statusAnalise = ['Julgamento Originario. ','Julgamento Recursal. ']
  self.rbox1 = wx.RadioBox(pnl, label = 'Estágio da Análise', pos = (10,120), choices=statusAnalise,  majorDimension = 2, style = wx.RA_SPECIFY_ROWS)
  self.rbox1.Bind(wx.EVT_RADIOBOX,self.onRadioBox1)

  tipoJulgamento = ['Recurso conhecido e provido. ','Recurso conhecido e nao provido. ','Recurso nao conhecido']
  self.combo = wx.ComboBox(pnl, choices=tipoJulgamento, pos=(10,200))
  self.combo.Bind(wx.EVT_COMBOBOX, self.onCombo)

  tipoProcedimento = ['Fiscalizacao ordinaria - PAF. ','Fiscalizacao Extraordinaria. ']
  self.combo1 = wx.ComboBox(pnl, choices=tipoProcedimento, pos=(10,230))
  self.combo1.Bind(wx.EVT_COMBOBOX, self.onCombo1)

  #Área de concatenação
  self.t3 = wx.TextCtrl(pnl,pos=(310,10),size = (180,170),style = wx.TE_MULTILINE)

  self.Centre() 
  self.Show(True)    

# Funcoes para eventos

def onRadioBox(self, e): 
  rb = self.rbox.GetStringSelection() 
  **self.t3.AppendText(str(rb).upper())** #here set text to 

def onRadioBox1(self, e):
  rb1 = self.rbox1.GetStringSelection()
  **self.t3.AppendText(str(rb1).upper())** #here

def onClear(self, e):
  self.t3.SetValue('')

def onExit(self, e):
  self.Destroy()

def onCopy(self, e):
  pyperclip.copy(self.t3.GetValue())

def onCombo(self, e):
  cbx = self.combo.GetValue()
  **self.t3.AppendText(str(cbx).upper())**  #here

def onCombo1(self, e):
  cbx1 = self.combo1.GetValue()
  **self.t3.AppendText(str(cbx1).upper())** #here

def onHelp(self, e):
  msgbox = wx.MessageBox('Feito por ²David Gesrob ® 2016-09-04', 'About...', wx.ICON_INFORMATION | wx.STAY_ON_TOP)


ex = wx.App() 
Ementor(None,'Ementor') 
ex.MainLoop()

Tags: posselfbinddefchoicesevtmenuwx
1条回答
网友
1楼 · 发布于 2024-09-30 14:36:18

您可以使用e.GetEventObject()访问发送事件的小部件

然后您可以使用isinstance来识别小部件类型

def onSelect(self, e):

  widget = e.GetEventObject()

  if isinstance(widget, wx.RadioBox):
      self.t3.AppendText(str(widget.GetStringSelection()).upper())
  elif isinstance(widget, wx.ComboBox):
      self.t3.AppendText(str(widget.GetValue()).upper())

现在,您可以将其与任何RadioBox和ComboBox一起使用

相关问题 更多 >