在按钮上单击打开wxpython TextEntryDialog并从用户获取多个输入

时间:2017-06-27 06:18:01

标签: python user-interface input dialog wxpython

我想在用户点击按钮时打开TextEntryDialog。所以,如果我在父框架中有一个按钮,我将以这种方式绑定:

      self.Bind(wx.EVT_BUTTON, self.OnAddNew, self.add_new_btn)

现在,当用户点击按钮add_new时,我必须打开一个TextEntryDialog。我想让textentrydialog像这样做  Python, Using wxPython to get multiple input from user

我该怎么做?我需要在`def OnAddNew(self,event)中粘贴该代码: 以下是我的代码的pastebin链接:https://pastebin.com/UEYscgFa 我在函数内部创建了类,所以可以这样做吗?

1 个答案:

答案 0 :(得分:1)

NO!
GetData本身就是一个班级 该代码已经为您提供了该方法。 MyFrame只是昙花一现,创建一个独立的工作示例。

def OnButton(self,event):
    dlg = GetData(parent = self.panel) 
    dlg.ShowModal()
    if dlg.result_name:
        self.log.AppendText("Name: "+dlg.result_name+"\n")
        self.log.AppendText("Surname: "+dlg.result_surname+"\n")
        self.log.AppendText("Nickname: "+dlg.result_nickname+"\n")
    else:
        self.log.AppendText("No Input found\n")
    dlg.Destroy()

编辑:我不明白我的评论中的说明在哪里躲过了你,但对于我的罪过,这里是你的代码在评论中被清理和编辑。

import sqlite3
import wx
import os

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(1000,800))
        self.inter_list = list()
        self.plot_list = list()
        self.InitUI()
        self.Layout()
        self.Centre()
        self.Show()

    def InitUI(self):
        self.p = wx.Panel(self)
        bs = wx.BoxSizer(wx.VERTICAL)
        gs = wx.GridSizer(10, 18, 5, 5)
        bs.Add(gs, 1, wx.EXPAND)
        self.search_btn=wx.Button(self.p,-1,"Search!")
        self.search_btn.Bind(wx.EVT_BUTTON, self.OnSearch, self.search_btn)
        bs.Add(self.search_btn,0,wx.ALIGN_CENTER)
        self.p.SetSizer(bs)

    def OnSearch(self, event):
        dlg = GetData(parent = self.p)
        dlg.ShowModal()
        if dlg.result_name:
            print "Name: "+dlg.result_name+"\n"
            print "Surname: "+dlg.result_surname+"\n"
            print "Nickname: "+dlg.result_nickname+"\n"
        else:
            print "No Input found\n"
        dlg.Destroy()

class GetData(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, wx.ID_ANY, "Name Input", size= (650,220))
        self.p = wx.Panel(self,wx.ID_ANY)
        self.lblname = wx.StaticText(self.p, label="Name", pos=(20,20))
        self.name = wx.TextCtrl(self.p, value="", pos=(110,20), size=(500,-1))
        self.lblsur = wx.StaticText(self.p, label="Surname", pos=(20,60))
        self.surname = wx.TextCtrl(self.p, value="", pos=(110,60), size=(500,-1))
        self.lblnick = wx.StaticText(self.p, label="Nickname", pos=(20,100))
        self.nickname = wx.TextCtrl(self.p, value="", pos=(110,100), size=(500,-1))
        self.saveButton =wx.Button(self.p, label="Save", pos=(110,160))
        self.closeButton =wx.Button(self.p, label="Cancel", pos=(210,160))
        self.saveButton.Bind(wx.EVT_BUTTON, self.SaveConnString)
        self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
        self.Bind(wx.EVT_CLOSE, self.OnQuit)
        self.Show()

    def OnQuit(self, event):
        self.result_name = None
        self.Destroy()

    def SaveConnString(self, event):
        self.result_name = self.name.GetValue()
        self.result_surname = self.surname.GetValue()
        self.result_nickname = self.nickname.GetValue()
        self.Destroy()

app = wx.App()
Example(None, title = 'Raman Spectroscopy Database')
app.MainLoop()