How to use python graphical interface wxPython

There are three main types of GUI libraries: tkinter, wxPython and PyQt5. The following are mainly instructions for wxPython.

The following operations are performed on win10 + pycharm

  1. Installation of wxPython:

    	pip install wxPython
    

    There are also two dependency packages installed: six and pilot

  2. Easy to use: The variable app is our program.

    Second, we must pay attention to the case, especially the S of Show

    	import wx
    	app = wx.App()
    	frame = wx.Frame(None, title = "Hello,world!")
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  3. Further, we can add a button: This is implemented by defining a MyFrame class that inherits from the wx.Frame class.

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        self.button = wx.Button(self, label = "hello")
    
    	app = wx.App()
    	frame = MyFrame(None, title = "Hello,world!")
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  4. But there's a problem. This button is too big and full of the whole interface, so we can further set the size and layout of the button:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        self.btn1 = wx.Button(self, label="hello1", pos=(10, 50), size=(100, 50))
    	        self.btn2 = wx.Button(self, label="hello2", pos=(120, 50), size=(100, 50))
    
    	app = wx.App()
    	frame = MyFrame(None, title="Hello,world!")
    	frame.Show()
    	app.MainLoop()
    

    Where pos is the position parameter, the first represents the distance from the left boundary, and the second represents the distance from the upper boundary; size is the size parameter of the button, the first represents the width, and the second represents the height.

    It should be noted that this button is the button under the absolute layout. That is to say, no matter how small you make the window, the button will not change with the size of the window

  5. Although we have buttons, we need to add focus at this time:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        panel = wx.Panel(self)
    	        self.btn1 = wx.Button(panel, label="hello1", pos=(10, 50), size=(100, 50))
    	        self.btn2 = wx.Button(panel, label="hello2", pos=(120, 50), size=(100, 50))
    
    	app = wx.App()
    	frame = MyFrame(None, title="Hello,world!")
    	frame.Show()
    	app.MainLoop()	
    

    The interface after operation is as follows:

    By adding wx.Panel, we can click the corresponding button with the mouse, or use Tab to move to the next button.

  6. Now it's OK to click, but it's not OK if there is no response, so we bind the button to the corresponding event, that is, respond to the button event:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        panel = wx.Panel(self)
    	        self.btn1 = wx.Button(panel, label="hello1", pos=(10, 50), size=(100, 50))
    	        self.btn2 = wx.Button(panel, label="hello2", pos=(120, 50), size=(100, 50))
    	        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.btn1)
    	        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.btn2)
    	    def OnButton1(self, event):
    	        print("Hello,world!")
    	    def OnButton2(self, event):
    	            print("Hello,world!Hello,world!")
    
    	app = wx.App()
    	frame = MyFrame(None, title="Hello,world!")
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  7. The following describes the use of some controls:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        panel = wx.Panel(self)
    
    	        self.static_text = wx.StaticText(panel, label="static text", pos=(10, 10))
    
    	        self.text_1 = wx.TextCtrl(panel, value="Textbox", pos=(10, 40))
    	        self.text_2 = wx.TextCtrl(panel, value="Password box", style=wx.TE_PASSWORD, pos=(10, 70))
    	        self.text_3 = wx.TextCtrl(panel, value="readonly textbox ", style=wx.TE_READONLY, pos=(10, 100))
    	        self.text_4 = wx.TextCtrl(panel, value="Multiple rows\n Textbox", style=wx.TE_MULTILINE, pos=(10, 130))
    
    	        self.btn1 = wx.Button(panel, label="Modify window title", pos=(200, 10))
    	        self.btn2 = wx.Button(panel, label="Modify static text content", pos=(200, 40))
    	        self.btn3 = wx.Button(panel, label="Modify text box content", pos=(200, 70))
    	        self.btn4 = wx.Button(panel, label="Modify read-only text box content", pos=(200, 100))
    
    	        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.btn1)
    	        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.btn2)
    	        self.Bind(wx.EVT_BUTTON, self.OnButton3, self.btn3)
    	        self.Bind(wx.EVT_BUTTON, self.OnText1, self.btn4)
    
    	    def OnButton1(self, event):
    	        self.Title += 'Hello'
    	    def OnButton2(self, event):
    	        self.static_text.Label += 'Hi'
    	    def OnButton3(self, event):
    	        self.text_1.Value = 'Good'
    	        self.text_2.Value = 'GOOD'
    	        self.text_3.Value = 'Good\nbetter\nbest'
    	        self.text_4.Value = 'GOOD\nBetter\nBest'
    	    def OnText1(self, event):
    	        self.text_3.Value = self.text_1.Value
    
    	app = wx.App()
    	frame = MyFrame(None, title="Multi_Text", size=(500, 300))
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  8. Next let's look at the use of adding a picture control:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        self.bitmap = wx.StaticBitmap(self, pos=(10, 10))
    	        self.btn1 = wx.Button(self, label="001", pos=(300, 10))
    	        self.btn2 = wx.Button(self, label='002', pos=(300, 40))
    	        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.btn1)
    	        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.btn2)
    
    	    def OnButton1(self, event):
    	        self.bitmap.Bitmap = wx.Bitmap('002.jpeg')
    	    def OnButton2(self, event):
    	        self.bitmap.Bitmap = wx.Bitmap('001.jpg')
    
    	app = wx.App()
    	frame = MyFrame(None, title="Images", size=(500, 290))
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  9. Next, we solve the problem of relative layout. First, we can use BoxSizer to reduce the window without blocking the buttons:

    	import wx
    
    	class MyFrame(wx.Frame):
    	    def __init__(self, *args, **kw):
    	        wx.Frame.__init__(self, *args, **kw)
    	        sizer = wx.BoxSizer(wx.HORIZONTAL)
    	        self.bitmap = wx.StaticBitmap(self, bitmap=wx.Bitmap('001.jpg'))
    	        self.btn1 = wx.Button(self, label="001")
    	        self.btn2 = wx.Button(self, label='002')
    	        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.btn1)
    	        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.btn2)
    	        sizer.Add(self.btn1)
    	        sizer.Add(self.btn2)
    	        sizer.Add(self.bitmap)
    	        self.SetSizerAndFit(sizer)
    
    	    def OnButton1(self, event):
    	        self.bitmap.Bitmap = wx.Bitmap('002.jpeg')
    	    def OnButton2(self, event):
    	        self.bitmap.Bitmap = wx.Bitmap('001.jpg')
    
    	app = wx.App()
    	frame = MyFrame(None, title="Images", size=(500, 290))
    	frame.Show()
    	app.MainLoop()
    

    The interface after operation is as follows:

  10. When you pull a window, the buttons will always follow it in the same proportion:

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kw):
            wx.Frame.__init__(self, *args, **kw)
            self.btn1 = wx.Button(self, label="button1")
            self.btn2 = wx.Button(self, label='button2')
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.btn1, 1)
            sizer.Add(self.btn2, 3)
            self.SetSizerAndFit(sizer)
    
    app = wx.App()
    frame = MyFrame(None, title="vertical")
    frame.Show()
    app.MainLoop()
    

    The interface after operation is as follows:

  11. Of course, you can also add location information:

    sizer.Add(self.btn1, 1, wx.ALIGN_LEFT)
    sizer.Add(self.btn3, 2, wx.TEXT_ALIGNMENT_LEFT)
    sizer.Add(self.btn2, 3, wx.EXPAND)
    
  12. You can also add border distance:

     sizer.Add(self.btn1, 1, wx.ALIGN_LEFT|wx.ALL, 10)
     sizer.Add(self.btn3, 2, wx.TEXT_ALIGNMENT_LEFT|wx.LEFT|wx.RIGHT, 300)
     sizer.Add(self.btn2, 3, wx.EXPAND)
    
  13. You can also Add a block to occupy:

    sizer.Add((50, 60))
    
  14. You can also set the scale:

    sizer.Add(self.btn1, 1, wx.ALL, 30)
    sizer.Add(self.btn3, 1, wx.ALL, 30)
    sizer.Add(self.btn2, 1, wx.ALL, 30)
    sizer.Add((50, 60), 3)
    

    The interface after operation is as follows:

  15. Finally, let's look at a nested layout:

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kw):
            wx.Frame.__init__(self, *args, **kw)
            self.btn1 = wx.Button(self, label="button1")
            self.btn2 = wx.Button(self, label='button2')
            self.btn3 = wx.Button(self, label='button3')
            self.btn4 = wx.Button(self, label="button4")
            self.btn5 = wx.Button(self, label="button5")
            hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
            hsizer1.Add(self.btn1, 0, wx.ALL, 5)
            hsizer1.Add(self.btn2, 0, wx.ALL, 5)
            hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
            hsizer2.Add(self.btn3, 1, wx.ALL, 5)
            hsizer2.Add(self.btn4, 1, wx.ALL, 5)
            vsizer1 = wx.BoxSizer(wx.VERTICAL)
            vsizer1.Add(hsizer1, 0, wx.ALIGN_RIGHT)
            vsizer1.Add(hsizer2, 0, wx.EXPAND)
            vsizer1.Add(self.btn5, 1, wx.EXPAND)
            self.SetSizerAndFit(vsizer1)
    
    app = wx.App()
    frame = MyFrame(None, title="nesting BoxSizer")
    frame.Show()
    app.MainLoop()
    

    Then let's see the operation effect:

Tags: Programming Pycharm pip

Posted on Thu, 26 Mar 2020 06:58:51 -0400 by sspoke