Simple example of using wx.TextCtrl and display data after button click in wxpython - new to wx

后端 未结 1 1227
小鲜肉
小鲜肉 2021-02-04 10:12

I am learning python and trying out wxpython for UI development (dont have UI exp either). I have been able to create a frame with a panel, a button and a text input box. I wou

相关标签:
1条回答
  • To do any GUI interactions you have to bind events to the widgets. You basically tell the wxPython app which method (event handler) should be called when some event (button pressed) occurs.

    I would also consider learning sizers and using them for your layouts. I have changed your example a bit.

    import wx
    class ExampleFrame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent)
    
            self.panel = wx.Panel(self)     
            self.quote = wx.StaticText(self.panel, label="Your quote:")
            self.result = wx.StaticText(self.panel, label="")
            self.result.SetForegroundColour(wx.RED)
            self.button = wx.Button(self.panel, label="Save")
            self.lblname = wx.StaticText(self.panel, label="Your name:")
            self.editname = wx.TextCtrl(self.panel, size=(140, -1))
    
            # Set sizer for the frame, so we can change frame size to match widgets
            self.windowSizer = wx.BoxSizer()
            self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)        
    
            # Set sizer for the panel content
            self.sizer = wx.GridBagSizer(5, 5)
            self.sizer.Add(self.quote, (0, 0))
            self.sizer.Add(self.result, (0, 1))
            self.sizer.Add(self.lblname, (1, 0))
            self.sizer.Add(self.editname, (1, 1))
            self.sizer.Add(self.button, (2, 0), (1, 2), flag=wx.EXPAND)
    
            # Set simple sizer for a nice border
            self.border = wx.BoxSizer()
            self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)
    
            # Use the sizers
            self.panel.SetSizerAndFit(self.border)  
            self.SetSizerAndFit(self.windowSizer)  
    
            # Set event handlers
            self.button.Bind(wx.EVT_BUTTON, self.OnButton)
    
        def OnButton(self, e):
            self.result.SetLabel(self.editname.GetValue())
    
    app = wx.App(False)
    frame = ExampleFrame(None)
    frame.Show()
    app.MainLoop()
    
    0 讨论(0)
提交回复
热议问题