[英]Event get value in wxpython
I have a problem with using wxpython. 我在使用wxpython时遇到问题。 I try to get a value from a column and I want to output to another column too after I hit a button. 我尝试从列中获取值,并且在按下按钮后我也想输出到另一列。 However, I dont know what I code to add to make this work. 但是,我不知道我要添加什么代码才能使其工作。
My current code is like this: 我目前的代码是这样的:
import wx
class scarlett(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(400, 350))
panel = wx.Panel(self, -1)
sizer = wx.GridBagSizer(4, 4)
text1 = wx.StaticText(panel, -1, 'Scarlett')
sizer.Add(text1, (0, 0), flag = wx.TOP | wx.LEFT | wx.BOTTOM, border=5)
tc = wx.TextCtrl(panel, -1)
sizer.Add(tc, (1, 0), (1, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
text2 = wx.StaticText(panel, -1, 'You')
sizer.Add(text2, (2, 0), flag = wx.TOP | wx.LEFT | wx.RIGHT, border = 5 )
list = wx.ListBox(panel, -1, style = wx.LB_ALWAYS_SB)
sizer.Add(list, (3, 0), (3, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
buttonOk = wx.Button(panel, -1, 'Ok', size=(90, 28))
buttonCancel = wx.Button(panel, -1, 'Cancel', size=(90, 28))
sizer.Add(buttonOk, (7, 1))
sizer.Add(buttonCancel, (7, 2), flag = wx.RIGHT | wx.BOTTOM, border = 5)
self.Bind(wx.EVT_BUTTON, self.OnQuit, id=buttonCancel.GetId())
sizer.AddGrowableCol(0)
sizer.AddGrowableRow(5)
sizer.AddGrowableRow(6)
sizer.SetEmptyCellSize((1, 1))
panel.SetSizer(sizer)
self.Center()
self.Show(True)
def OnQuit(self, event):
self.Close()
app = wx.App()
scarlett(None, -1, 'scarlett')
app.MainLoop()
If I understand correctly, you are wanting to copy text from the text control to the list box? 如果我理解正确,您想要将文本从文本控件复制到列表框? If that is correct then you simply need to save a reference to the controls so you can access them later in the event handler functions. 如果这是正确的,那么您只需要保存对控件的引用,以便稍后在事件处理函数中访问它们。 For example: 例如:
import wx
class scarlett(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(400, 350))
panel = wx.Panel(self, -1)
sizer = wx.GridBagSizer(4, 4)
text1 = wx.StaticText(panel, -1, 'Scarlett')
sizer.Add(text1, (0, 0), flag = wx.TOP | wx.LEFT | wx.BOTTOM, border=5)
self.tc = wx.TextCtrl(panel, -1)
sizer.Add(self.tc, (1, 0), (1, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
text2 = wx.StaticText(panel, -1, 'You')
sizer.Add(text2, (2, 0), flag = wx.TOP | wx.LEFT | wx.RIGHT, border = 5 )
self.listbox = wx.ListBox(panel, -1, style = wx.LB_ALWAYS_SB)
sizer.Add(self.listbox, (3, 0), (3, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
buttonOk = wx.Button(panel, -1, 'Ok', size=(90, 28))
buttonCancel = wx.Button(panel, -1, 'Cancel', size=(90, 28))
sizer.Add(buttonOk, (7, 1))
sizer.Add(buttonCancel, (7, 2), flag = wx.RIGHT | wx.BOTTOM, border = 5)
self.Bind(wx.EVT_BUTTON, self.OnQuit, buttonCancel)
self.Bind(wx.EVT_BUTTON, self.OnCopyText, buttonOk)
sizer.AddGrowableCol(0)
sizer.AddGrowableRow(5)
sizer.AddGrowableRow(6)
sizer.SetEmptyCellSize((1, 1))
panel.SetSizer(sizer)
self.Center()
self.Show(True)
def OnQuit(self, event):
self.Close()
def OnCopyText(self, event):
self.listbox.Append(self.tc.GetValue())
app = wx.App()
frm = scarlett(None, -1, "scarlett")
app.MainLoop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.