2

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
  • Remember! you can only accept one answer, not two! Commented Jul 8, 2017 at 20:02

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

9 Comments

that gives me error: NameError: global name 'GetData' is not defined, i want to make that same textentrydialog and finally i will send that text to use in my sqlite query
1. Where is the GetData class you have omitted it from your code entirely and 2. You called your panel p so the call should be dlg = GetData(parent = self.p) and to achieve that you need to change all references to p to be self.p or perhaps something a little more descriptive.
can i create that GetData class inside OnButton function?
It says global name p is not defibned
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.