2012-05-09 33 views
5

Tôi mới sử dụng Python và WX. Tôi đã tạo một hộp thoại thử nghiệm đơn giản được hiển thị bên dưới để nhắc người dùng bằng hộp tổ hợp. Tôi muốn nắm bắt giá trị từ combox trong chương trình chính của tôi. Làm thế nào để tôi gọi nó từ chương trình chính của tôi?Python WX - Trả về đầu vào của người dùng từ wx Hộp thoại

Đây là cách tôi đã purposing gọi nó là hiển thị hộp thoại nhưng hiện không nắm bắt được giá trị từ combobox:

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

Tên của tập tin Dialog là "nổi bật". Bên dưới là mã:

#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 

Trả lời

9

Có rất nhiều ví dụ về hộp thoại. Dưới đây là một vài:

Về cơ bản, tất cả các bạn cần làm là nhanh chóng thoại của bạn, hiển thị nó và sau đó trước khi bạn đóng nó, chiết xuất giá trị. Cách thông thường để làm điều đó là một cái gì đó như thế này:

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

Cập nhật: Dưới đây là một full-fledged hơn ví dụ:

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() 
+0

Mike, Cảm ơn bạn đã trả lời. Khi tôi sử dụng res = myDlg.ShowModal() nó không hoạt động nhưng nếu tôi thay đổi nó thành res <> myDlg.ShowModal() nó hoạt động. Bạn có biết tại sao? Cảm ơn. – user1314011

+0

Nó sẽ hoạt động. Tôi cần một ví dụ runnable và traceback để biết những gì đang xảy ra. –

+0

nó sẽ là tốt để thêm những gì mã cần phải được trong hộp thoại tùy chỉnh cũng như các ứng dụng chính. Câu hỏi OPs không được đề cập trong ví dụ về hộp thoại tùy chỉnh zetcode – Anake

Các vấn đề liên quan