0

I have the following code for a small GUI using wxPython. Now I want to write the userinput in inputone, inputtwo etc in variables to work later with. I have no idea how to do that.

   import wx
    
    
    class Utform(wx.Frame):
    
        def __init__(self):
    
            wx.Frame.__init__(self, None, wx.ID_ANY, title='Tool')
    
            
            self.panel = wx.Panel(self, wx.ID_ANY)
    
            title = wx.StaticText(self.panel, wx.ID_ANY, 'Upgrade')
    
            labelone = wx.StaticText(self.panel, wx.ID_ANY, 'inputone', size=(100, 20))
            inputtxtone = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(200, 20))
    
            labeltwo = wx.StaticText(self.panel, wx.ID_ANY, 'inputtwo', size=(100, 20))
            inputtxttwo = wx.TextCtrl(self.panel, wx.ID_ANY, 'YYYY-MM-DD', size=(200, 20))
    
            labelthree = wx.StaticText(self.panel, wx.ID_ANY, 'inputthree', size=(100, 20))
            inputtxtthree = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(200, 20))
    
            labelfour = wx.StaticText(self.panel, wx.ID_ANY, 'inputfour', size=(100, 20))
            inputtxtfour = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(200, 20))
    
            labelfive = wx.StaticText(self.panel, wx.ID_ANY, 'inputfive', size=(100, 20))
            inputtxtfive = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(200, 20))
    
            okbtn = wx.Button(self.panel, wx.ID_ANY, 'OK')
            cancelbtn = wx.Button(self.panel, wx.ID_ANY, 'Cancel')
            self.Bind(wx.EVT_BUTTON, self.onok, okbtn)
            self.Bind(wx.EVT_BUTTON, self.oncancel, cancelbtn)
    
            
            topsizer = wx.BoxSizer(wx.VERTICAL)
            titlesizer = wx.BoxSizer(wx.VERTICAL)
            inputfoursizer = wx.BoxSizer(wx.HORIZONTAL)
            inputfivesizer = wx.BoxSizer(wx.HORIZONTAL)
            inputonesizer = wx.BoxSizer(wx.HORIZONTAL)
            inputtwosizer = wx.BoxSizer(wx.HORIZONTAL)
            inputthreesizer = wx.BoxSizer(wx.HORIZONTAL)
    
            btnsizer = wx.BoxSizer(wx.HORIZONTAL)
    
            titlesizer.Add(title, 0, wx.ALL, 5)
    
            inputonesizer.Add(labelone, 0, wx.ALL, 5)
            inputonesizer.Add(inputtxtone, 0, wx.ALL | wx.EXPAND, 5)
    
            inputtwosizer.Add(labeltwo, 0, wx.ALL, 5)
            inputtwosizer.Add(inputtxttwo, 0, wx.ALL | wx.EXPAND, 5)
    
            inputthreesizer.Add(labelthree, 0, wx.ALL, 5)
            inputthreesizer.Add(inputtxtthree, 0, wx.ALL | wx.EXPAND, 5)
    
            inputfoursizer.Add(labelfour, 0, wx.ALL, 5)
            inputfoursizer.Add(inputtxtfour, 0, wx.ALL | wx.EXPAND, 5)
    
            inputfivesizer.Add(labelfive, 0, wx.ALL, 5)
            inputfivesizer.Add(inputtxtfive, 0, wx.ALL | wx.EXPAND, 5)
    
            btnsizer.Add(okbtn, 1, wx.ALL, 5)
            btnsizer.Add(cancelbtn, 1, wx.ALL, 5)
    
            topsizer.Add(titlesizer, 0, wx.CENTER)
            topsizer.Add(wx.StaticLine(self.panel, ), 0, wx.ALL | wx.EXPAND, 5)
    
            topsizer.Add(inputfoursizer, 0, wx.ALL | wx.EXPAND, 5)
            topsizer.Add(inputfivesizer, 0, wx.ALL | wx.EXPAND, 5)
            topsizer.Add(inputonesizer, 0, wx.ALL | wx.EXPAND, 5)
            topsizer.Add(inputtwosizer, 0, wx.ALL | wx.EXPAND, 5)
            topsizer.Add(inputthreesizer, 0, wx.ALL | wx.EXPAND, 5)
            topsizer.Add(btnsizer, 0, wx.ALL | wx.EXPAND, 5)
    
            self.panel.SetSizer(topsizer)
            topsizer.Fit(self)
    
            customerpath = os.path.abspath(".")
            self.CreateStatusBar()
            self.SetStatusText(customerpath)
    
        def onok(self, event):
            # MAKE UPGRADE
            customerpath = os.path.abspath(".")
            wx.MessageBox('OK')
            customerpath = os.path.abspath(".")
            print("path")
    
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        frame = Utform().Show()
        app.MainLoop()
    app.MainLoop()
0x01_PH
  • 126
  • 10
  • Does this answer your question? [Getting String from A TextCtrl Box](https://stackoverflow.com/questions/2046338/getting-string-from-a-textctrl-box) – ChatterOne Sep 09 '20 at 07:28
  • Arguably they are already variables. You can access them via `GetValue()`. So, `inputtxtone.GetValue()` would return whatever is in that variable. One issue, is that you are declaring them as `local` variables, to be able to access them easily in another function, declare them as `instance` variables i.e. `self.inputtxtone` then in your event function for that input `myvariableone = self.inputtxtone.GetValue()`. You can drag it out of the eventobject but that's another story. – Rolf of Saxony Sep 09 '20 at 09:38

1 Answers1

0

Further to my comment, play with this code to see how some of this works.
The event you choose for the TextCtrl callback trigger is important, as is the style.

import wx
import os

class Utform(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, wx.ID_ANY, title='Tool')

        
        self.panel = wx.Panel(self, wx.ID_ANY)

        title = wx.StaticText(self.panel, wx.ID_ANY, 'Upgrade')

        labelone = wx.StaticText(self.panel, wx.ID_ANY, 'inputone', size=(100, 20))
        self.inputtxtone = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(200, 20))

        labeltwo = wx.StaticText(self.panel, wx.ID_ANY, 'inputtwo', size=(100, 20))
        inputtxttwo = wx.TextCtrl(self.panel, wx.ID_ANY, 'YYYY-MM-DD', size=(200, 20))

        labelthree = wx.StaticText(self.panel, wx.ID_ANY, 'inputthree', size=(100, 20))
        self.inputtxtthree = wx.TextCtrl(self.panel, wx.ID_ANY, '', style=wx.TE_PROCESS_ENTER, size=(200, 20))
            
        okbtn = wx.Button(self.panel, wx.ID_ANY, 'OK')
        cancelbtn = wx.Button(self.panel, wx.ID_ANY, 'Cancel')

        self.inputtxtone.Bind(wx.EVT_KILL_FOCUS, self.input1)
        inputtxttwo.Bind(wx.EVT_TEXT, self.input2)
        self.inputtxtthree.Bind(wx.EVT_TEXT_ENTER, self.input3)

        self.Bind(wx.EVT_BUTTON, self.onok, okbtn)
        self.Bind(wx.EVT_BUTTON, self.oncancel, cancelbtn)

        
        topsizer = wx.BoxSizer(wx.VERTICAL)
        titlesizer = wx.BoxSizer(wx.VERTICAL)
        inputonesizer = wx.BoxSizer(wx.HORIZONTAL)
        inputtwosizer = wx.BoxSizer(wx.HORIZONTAL)
        inputthreesizer = wx.BoxSizer(wx.HORIZONTAL)

        btnsizer = wx.BoxSizer(wx.HORIZONTAL)

        titlesizer.Add(title, 0, wx.ALL, 5)

        inputonesizer.Add(labelone, 0, wx.ALL, 5)
        inputonesizer.Add(self.inputtxtone, 0, wx.ALL | wx.EXPAND, 5)

        inputtwosizer.Add(labeltwo, 0, wx.ALL, 5)
        inputtwosizer.Add(inputtxttwo, 0, wx.ALL | wx.EXPAND, 5)

        inputthreesizer.Add(labelthree, 0, wx.ALL, 5)
        inputthreesizer.Add(self.inputtxtthree, 0, wx.ALL | wx.EXPAND, 5)
        
        btnsizer.Add(okbtn, 1, wx.ALL, 5)
        btnsizer.Add(cancelbtn, 1, wx.ALL, 5)

        topsizer.Add(titlesizer, 0, wx.CENTER)
        topsizer.Add(wx.StaticLine(self.panel, ), 0, wx.ALL | wx.EXPAND, 5)

        topsizer.Add(inputonesizer, 0, wx.ALL | wx.EXPAND, 5)
        topsizer.Add(inputtwosizer, 0, wx.ALL | wx.EXPAND, 5)
        topsizer.Add(inputthreesizer, 0, wx.ALL | wx.EXPAND, 5)
        topsizer.Add(btnsizer, 0, wx.ALL | wx.EXPAND, 5)

        self.panel.SetSizer(topsizer)

        customerpath = os.path.abspath(".")
        self.CreateStatusBar()
        self.SetStatusText(customerpath)

    def input1(self, event):
        print(self.inputtxtone.GetValue())
        
    def input2(self, event):
        obj = event.GetEventObject()
        print(obj.GetValue())
        
    def input3(self, event):
        print(self.inputtxtthree.GetValue())
        
    def oncancel(self, event):
        print("cancelled")
        
    def onok(self, event):
        # MAKE UPGRADE
        customerpath = os.path.abspath(".")
        wx.MessageBox('OK')
        customerpath = os.path.abspath(".")
        print("path", customerpath)

if __name__ == '__main__':
    app = wx.App()
    frame = Utform().Show()
    app.MainLoop()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Yes! It works. Thanks for the example. Now i have to seperate the single parts and integrate it to the existing script. Thank you so much! – 0x01_PH Sep 09 '20 at 10:31
  • @0x01_PH Welcome to StackOverflow. – Rolf of Saxony Sep 09 '20 at 11:11
  • :D yeah i know but your code example helped me a lot to understand how it works! thank you so much - learned a lot today (that's also stackoverflow ;) ) – 0x01_PH Sep 09 '20 at 11:13