这里代码写入用户选择的字符串的一部分。在每个事件之后,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()
答案 0 :(得分:0)
您可以使用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一起使用