gpt4 book ai didi

Python WX - 从 wx 对话框返回用户输入

转载 作者:太空狗 更新时间:2023-10-29 22:19:57 25 4
gpt4 key购买 nike

我是 Python 和 WX 的新手。我创建了一个简单的测试对话框,如下所示,用组合框提示用户。我想从我的主程序中的组合框中获取值。我如何从我的主程序中调用它?

这就是我打算调用它来显示对话框但当前不从组合框中捕获值的方式:

    import highlight
highlight.create(self).Show(True)
a = highlight.OnComboBox1Combobox(self)
print a

对话框文件的名称是“highlight”。下面是代码:

#Boa:Dialog:Dialog2

import wx

def create(parent):
return Dialog2(parent)

[wxID_DIALOG2, wxID_DIALOG2COMBOBOX1, wxID_DIALOG2STATICTEXT1,
] = [wx.NewId() for _init_ctrls in range(3)]

class Dialog2(wx.Dialog):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Dialog.__init__(self, id=wxID_DIALOG2, name='', parent=prnt,
pos=wx.Point(264, 140), size=wx.Size(400, 485),
style=wx.DEFAULT_DIALOG_STYLE, title='Dialog2')
self.SetClientSize(wx.Size(384, 447))

self.comboBox1 = wx.ComboBox(choices=['test1', 'test2'],
id=wxID_DIALOG2COMBOBOX1, name='comboBox1', parent=self,
pos=wx.Point(120, 16), size=wx.Size(130, 21), style=0,
value=u'wining\n')
self.comboBox1.SetToolTipString(u'comboBox1')
self.comboBox1.SetLabel(u'wining\n')
self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnComboBox1Combobox,
id=wxID_DIALOG2COMBOBOX1)

self.staticText1 = wx.StaticText(id=wxID_DIALOG2STATICTEXT1,
label=u'test', name='staticText1', parent=self, pos=wx.Point(88,
16), size=wx.Size(19, 13), style=0)

def __init__(self, parent):
self._init_ctrls(parent)


##print get_selection
##print get_selection1

def OnComboBox1Combobox(self, event):
get_selection = self.comboBox1.GetValue()
return get_selection

最佳答案

那里有很多对话示例。这里有一对:

基本上,您需要做的就是实例化对话框,显示它,然后在关闭它之前提取值。典型的做法是这样的:

myDlg = MyDialog()
res = myDlg.ShowModal()
if res == wx.ID_OK:
value = myDlg.myCombobox.GetValue()
myDlg.Destroy()

更新:这是一个更完整的示例:

import wx

########################################################################
class MyDialog(wx.Dialog):
""""""

#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="Dialog")

self.comboBox1 = wx.ComboBox(self,
choices=['test1', 'test2'],
value="")
okBtn = wx.Button(self, wx.ID_OK)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.comboBox1, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(okBtn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(sizer)

########################################################################
class MainProgram(wx.Frame):
""""""

#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Main Program")
panel = wx.Panel(self)

btn = wx.Button(panel, label="Open dialog")
btn.Bind(wx.EVT_BUTTON, self.onDialog)

self.Show()

#----------------------------------------------------------------------
def onDialog(self, event):
""""""
dlg = MyDialog()
res = dlg.ShowModal()
if res == wx.ID_OK:
print dlg.comboBox1.GetValue()
dlg.Destroy()

if __name__ == "__main__":
app = wx.App(False)
frame = MainProgram()
app.MainLoop()

关于Python WX - 从 wx 对话框返回用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10520788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com