Sizer Management in wxpython is hard for beginners to understand. The problems I encountered during my study are recorded for reference and reference.
When adding textctrl, a problem occurs. The muti-line text control cannot always change with the frame and the display is incomplete. Original code: Create a frame and add the panel control to the frame. Create a boxsizer and associate it with the Panel. Create a textctrl control and two button controls and add them to sizer for management. ImplementationCodeAs follows:
1 Import WX
2
3 Class Mainframe (wx. Frame ):
4 Def _ Init __ (Self ):
5 Wx. Frame._ Init __ (Self, none,-1, " Xforwartopcalander " , Size = (100,200 ))
6 MB = self. createmenubar ()
7 Self. setmenubar (MB)
8 Self. createstatusbar ()
9 Self. createpanel ()
10
11 Def Createmenubar (Self ):
12 MB = wx. menubar ()
13
14 Menufile = wx. menu ()
15 MB. append (menufile, " & File " )
16 Menufile. append (-1, " & Open " )
17 Menufile. append (-1, " & Close " )
18
19 Menuedit = wx. menu ()
20 MB. append (menuedit, " & Edit " )
21 Menuedit. append (-1, " & Redo " )
22 Menuedit. append (-1, " & Undo " )
23
24 Menuhelp = wx. menu ()
25 MB. append (menuhelp, " & Help " )
26 Menuhelp. append (-1, " A & bout me " )
27 Return MB
28
29 Def Createpanel (Self ):
30 """ Create panel container """
31 Panel = wx. Panel (self,-1)
32 Panel. setbackgroundcolour ( " White " )
33
34 Sizer = wx. boxsizer (wx. Vertical)
35
36 TEXT = wx. statictext (panel,-1, " Muti-line test " )
37 # Text. setinsertionpoint (0)
38
39 OK = wx. Button (self, 20, "OK", (20, 80) // the user cannot be added to the Panel container.
40 canel = wx. Button (self, 20, "canel", (30, 80) // the sizer layout management is incorrect. display the self --> Panel properly.
41
42 Sizer. Add (text, 1, flag = wx. Left | wx. Right | wx. Expand, border = 1)
43 Sizer. Add (canel, 0)
44 Sizer. Add (OK, 0)
45 Panel. setsizer (Sizer)
46
47 If _ Name __ = " _ Main __ " :
48 APP = wx. pysimpleapp ()
49 Window = mainframe ()
50 Window. Show ()
51 App. mainloop ()
However, the running result is not as complete as I imagined. textctrl is not displayed, and only one button is displayed. I checked it for half a day.
After referring to the others, the modifications are as follows and finally displayed as I want:
1 Import WX
2
3 Class Sizerframe (wx. Frame ):
4 Def _ Init __ (Self ):
5 Wx. Frame. _ Init __ (Self, none, Title = " Sizer test frame " )
6 Panel = wx. Panel (self,-1)
7 Panel. setbackgroundcolour ( " Black " )
8
9 Sizer = wx. boxsizer (wx. Vertical)
10
11 Box1 = wx. boxsizer (wx. Horizontal)
12 TEXT = wx. textctrl (panel,-1, " Muti Line Text Test " , Style = wx. te_multiline)
13 Box1.add (text, proportion = 1, flag = wx. Expand)
14 Sizer. Add (box1, proportion = 1, flag = wx. Left | wx. Right | wx. Expand, border = 2)
15
16 OK _button = wx. Button (panel, wx. id_abort, " OK " )
17 Canel_button = wx. Button (panel, wx. id_apply, " Canel " )
18
19 Sizer. Add (OK _button, flag = wx. Expand)
20 Sizer. Add (canel_button, flag = wx. Expand)
21
22 Panel. setsizer (Sizer)
23
24
25 If _ Name __ = " _ Main __ " :
26 APP = wx. pysimpleapp ()
27 Window = sizerframe ()
28 Window. Show ()
29 App. mainloop ()