On button click open wxpython TextEntryDialog and

2019-07-11 03:12发布

I want to open a TextEntryDialog, when user clicks the button. So if i have a button in the parent frame which i am going to bind this way:

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

Now i have to open a TextEntryDialog when user clicks the button add_new. I want to make textentrydialog somewthing like this Python, Using wxPython to get multiple input from user

How can i do that? Do i need to just paste that code in ` def OnAddNew(self, event): Here is the pastebin link to my code: https://pastebin.com/UEYscgFa I have created class inside a function, so is it possible to do in that way?

1条回答
可以哭但决不认输i
2楼-- · 2019-07-11 04:06

NO!
GetData is a class in its own right.
That code already provides you with the method. The MyFrame is all fluff, to create a standalone working example.

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

Edit: I don't understand where the instructions in my comments eluded you but for my sins, here is your code cleaned up and edited as in the comments.

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()
查看更多
登录 后发表回答