使用wxPython将用户输入作为变量

问题描述

对于使用wxPython的小型GUI,我具有以下代码。现在,我想在变量inputoneinputtwo等中编写userinput以便以后使用。我不知道该怎么做。

   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,'Upgrade')
    
            labelone = wx.StaticText(self.panel,'inputone',size=(100,20))
            inputtxtone = wx.TextCtrl(self.panel,'',size=(200,20))
    
            labeltwo = wx.StaticText(self.panel,'inputtwo',20))
            inputtxttwo = wx.TextCtrl(self.panel,'YYYY-MM-DD',20))
    
            labelthree = wx.StaticText(self.panel,'inputthree',20))
            inputtxtthree = wx.TextCtrl(self.panel,20))
    
            labelfour = wx.StaticText(self.panel,'inputfour',20))
            inputtxtfour = wx.TextCtrl(self.panel,20))
    
            labelfive = wx.StaticText(self.panel,'inputfive',20))
            inputtxtfive = wx.TextCtrl(self.panel,20))
    
            okbtn = wx.Button(self.panel,'OK')
            cancelbtn = wx.Button(self.panel,'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,wx.ALL,5)
    
            inputonesizer.Add(labelone,5)
            inputonesizer.Add(inputtxtone,wx.ALL | wx.EXPAND,5)
    
            inputtwosizer.Add(labeltwo,5)
            inputtwosizer.Add(inputtxttwo,5)
    
            inputthreesizer.Add(labelthree,5)
            inputthreesizer.Add(inputtxtthree,5)
    
            inputfoursizer.Add(labelfour,5)
            inputfoursizer.Add(inputtxtfour,5)
    
            inputfivesizer.Add(labelfive,5)
            inputfivesizer.Add(inputtxtfive,5)
    
            btnsizer.Add(okbtn,1,5)
            btnsizer.Add(cancelbtn,5)
    
            topsizer.Add(titlesizer,wx.CENTER)
            topsizer.Add(wx.StaticLine(self.panel,),5)
    
            topsizer.Add(inputfoursizer,5)
            topsizer.Add(inputfivesizer,5)
            topsizer.Add(inputonesizer,5)
            topsizer.Add(inputtwosizer,5)
            topsizer.Add(inputthreesizer,5)
            topsizer.Add(btnsizer,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()

解决方法

在我的评论之前,请使用以下代码查看其中的一些工作原理。
event回调触发器选择的TextCtrlstyle一样重要。

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,'Upgrade')

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

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

        labelthree = wx.StaticText(self.panel,'inputthree',20))
        self.inputtxtthree = wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,20))
            
        okbtn = wx.Button(self.panel,'OK')
        cancelbtn = wx.Button(self.panel,'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,wx.ALL,5)

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

        inputtwosizer.Add(labeltwo,5)
        inputtwosizer.Add(inputtxttwo,5)

        inputthreesizer.Add(labelthree,5)
        inputthreesizer.Add(self.inputtxtthree,5)
        
        btnsizer.Add(okbtn,1,5)
        btnsizer.Add(cancelbtn,5)

        topsizer.Add(titlesizer,wx.CENTER)
        topsizer.Add(wx.StaticLine(self.panel,),5)

        topsizer.Add(inputonesizer,5)
        topsizer.Add(inputtwosizer,5)
        topsizer.Add(inputthreesizer,5)
        topsizer.Add(btnsizer,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()