problem of switching panel in wxpython

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peteryang
    New Member
    • Dec 2019
    • 1

    problem of switching panel in wxpython

    I am recently trying to learn wxpythong and now I got a problem that when I destroy a panel and creat another one by a function, the panel doesn't shows up untill I change the size of my frame manually.


    Code:
    import wx
    import random
    class MyFrame(wx.Frame):
        num1=[0,0,0,0,0,0]
        num2=[0,0,0,0,0,0]
        opp=["-","-","-","-","-","-"]
        ans=[0,0,0,0,0,0]
        ansu=[0,0,0,0,0,0]
        oppr=int()
        score=int(0)
        for i in range (len (ans)):
            ans[i]=0
            oppr=random.randint(1,4)
            num1[i]= str(random.randint(0,99))
            num2[i]= str(random.randint(0,99))
            if oppr == 1:
                opp[i]="+"
            if oppr==2:
                opp[i]=="-"
            if oppr== 3:
                opp[i]="x"
            if oppr==4:
                opp[i]="/"
                a = int(random.randint(1,20))
                b = int(random.randint(1,20))
                c = int()
                c=a * b
                print(c)
                print(b)
                print(a)
                print(i)
                num1[i]=str(c)
                num2[i]=str(b)
                
                    
            
        def __init__(self):
            super().__init__(parent=None,title="new",size=(1000,1000),pos=(100,100))
            
            
            self.paneln = wx.Panel(parent=self)
            self.Refresh()
            self.paneln.SetBackgroundColour(wx.GREEN)
            vert=wx.BoxSizer(wx.VERTICAL)
            self.math=[
                wx.Bitmap("C:/Users/windows/Desktop/pi5oA6xKT.gif",wx.BITMAP_TYPE_GIF)
                ]
            self.image=wx.StaticBitmap(self.paneln,-1,self.math[0])
            vert.Add(self.image,proportion=2,flag=wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
            self.login=wx.Button(parent=self.paneln,id=101, label='login')
            vert.Add(self.login,proportion=2,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.TOP|wx.BOTTOM|wx.CENTER,border=100)
            self.login.SetBackgroundColour("PURPLE")
    
            self.paneln.SetSizer(vert)
            self.Bind(wx.EVT_BUTTON,self.newf,id=101)
    
        def newf (self,event):
    
            self.paneln.Destroy()
            
            self.panel= wx.Panel(self)
            self.panel.SetBackgroundColour(wx.GREEN)
            
            hs1=wx.BoxSizer()
            vs=wx.BoxSizer(wx.VERTICAL)
            self.vs2=wx.BoxSizer(wx.VERTICAL)
            vs3=wx.BoxSizer(wx.VERTICAL)
            hs2=wx.BoxSizer()
            hs3=wx.BoxSizer()
            hs4=wx.BoxSizer()
            hs5=wx.BoxSizer()
            hs6=wx.BoxSizer()
            hs7=wx.BoxSizer()
            hs8=wx.BoxSizer()
            hsm=wx.BoxSizer()
    
            
            self.title=wx.StaticText(parent=self.panel,label="Math Test")
            vs.Add(self.title,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.title2=wx.StaticText(parent=self.panel,label="practice test")
            vs.Add(self.title2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
    #############################################################################################
            self.test1_1=wx.StaticText(parent=self.panel,label=self.num1[0])
            hs1.Add(self.test1_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp1_1=wx.StaticText(parent=self.panel,label=self.opp[0])
            hs1.Add(self.test_opp1_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test1_2=wx.StaticText(parent=self.panel,label=self.num2[0])
            hs1.Add(self.test1_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs1.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans1=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=1)
            hs1.Add(self.ans1,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs1,proportion=1,flag=wx.CENTER,)
    #--------------------------------------------------------------------------------------------------
            self.test2_1=wx.StaticText(parent=self.panel,label=self.num1[1])
            hs2.Add(self.test2_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp2_1=wx.StaticText(parent=self.panel,label=self.opp[1])
            hs2.Add(self.test_opp2_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test2_2=wx.StaticText(parent=self.panel,label=self.num2[1])
            hs2.Add(self.test2_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs2.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans2=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=2)
            hs2.Add(self.ans2,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs2,proportion=1,flag=wx.CENTER,)
    #----------------------------------------------------------------------------------------------------
            self.test3_1=wx.StaticText(parent=self.panel,label=self.num1[2])
            hs3.Add(self.test3_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp3_1=wx.StaticText(parent=self.panel,label=self.opp[2])
            hs3.Add(self.test_opp3_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test3_2=wx.StaticText(parent=self.panel,label=self.num2[2])
            hs3.Add(self.test3_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs3.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans3=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=3)
            hs3.Add(self.ans3,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs3,proportion=1,flag=wx.CENTER,)
    #-------------------------------------------------------------------------------------------------------
            self.test4_1=wx.StaticText(parent=self.panel,label=self.num1[3])
            hs4.Add(self.test4_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp4_1=wx.StaticText(parent=self.panel,label=self.opp[3])
            hs4.Add(self.test_opp4_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test4_2=wx.StaticText(parent=self.panel,label=self.num2[3])
            hs4.Add(self.test4_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs4.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans4=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=4)
            hs4.Add(self.ans4,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs4,proportion=1,flag=wx.CENTER,)
    #-------------------------------------------------------------------------------------------------------
            self.test5_1=wx.StaticText(parent=self.panel,label=self.num1[4])
            hs5.Add(self.test5_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp5_1=wx.StaticText(parent=self.panel,label=self.opp[4])
            hs5.Add(self.test_opp5_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test5_2=wx.StaticText(parent=self.panel,label=self.num2[4])
            hs5.Add(self.test5_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs5.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans5=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=5)
            hs5.Add(self.ans5,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs5,proportion=1,flag=wx.CENTER,)
    #-------------------------------------------------------------------------------------------------------
            self.test6_1=wx.StaticText(parent=self.panel,label=self.num1[5])
            hs6.Add(self.test6_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_opp6_1=wx.StaticText(parent=self.panel,label=self.opp[5])
            hs6.Add(self.test_opp6_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test6_2=wx.StaticText(parent=self.panel,label=self.num2[5])
            hs6.Add(self.test6_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.test_e=wx.StaticText(parent=self.panel,label="=")
            hs6.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.ans6=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=6)
            hs6.Add(self.ans6,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs6,proportion=1,flag=wx.CENTER,)
    #######################################################################################################
    
            self.score1=wx.StaticText(parent=self.panel,label=str(self.score))
            hs7.Add(self.score1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.score2=wx.StaticText(parent=self.panel,label="/6")
            hs7.Add(self.score2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs7,proportion=1,flag=wx.CENTER,)
            #-------------------------------------------------------------------------------
            self.rep1=wx.StaticText(parent=self.panel,label=" [1] ")
            hs8.Add(self.rep1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.rep2=wx.StaticText(parent=self.panel,label=" [2] ")
            hs8.Add(self.rep2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.rep3=wx.StaticText(parent=self.panel,label=" [3] ")
            hs8.Add(self.rep3,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.rep4=wx.StaticText(parent=self.panel,label=" [4] ")
            hs8.Add(self.rep4,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.rep5=wx.StaticText(parent=self.panel,label=" [5] ")
            hs8.Add(self.rep5,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.rep6=wx.StaticText(parent=self.panel,label=" [6] ")
            hs8.Add(self.rep6,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            vs.Add(hs8,proportion=1,flag=wx.CENTER,)
    #########################################################################################################
            self.submit=wx.Button(parent=self.panel,id=50, label='submit')
            vs.Add(self.submit,proportion=2,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.submit.SetBackgroundColour("PURPLE")
    #-------------------------------------------------------------------------------------------------------
            self.next=wx.Button(parent=self.panel,id=49, label='next')
            vs.Add(self.next,proportion=2,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
            self.next.SetBackgroundColour("PURPLE")
    ############################################################################################
            self.teach=wx.StaticText(parent=self.panel,label="teacher section:")
            self.vs2.Add(self.teach,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
            self.teacher_gif=[
                wx.Bitmap("C:/Users/windows/Desktop/image.png",wx.BITMAP_TYPE_PNG)
                ]
            self.image=wx.StaticBitmap(self.panel,-1,self.teacher_gif[0])
            self.vs2.Add(self.image,proportion=5,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
            self.pas=wx.StaticText(parent=self.panel,label="teacher password:")
            self.vs2.Add(self.pas,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
            self.pasw=wx.TextCtrl(self.panel,style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER,id=51)
            self.vs2.Add(self.pasw,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
            hsm.Add(vs,proportion=1,flag=wx.CENTER)
            hsm.Add(self.vs2,proportion=1,flag=wx.CENTER)
    
    
    
    
    
    
            self.panel.SetSizer(hsm)
            hsm.Layout()
            self.Refresh()
            self.Bind(wx.EVT_BUTTON,self.on_click,id=10,id2=50)
            self.Bind(wx.EVT_TEXT_ENTER,self.teach_pass,id=51)
            self.Bind(wx.EVT_TEXT,self.Set_Ans,id=0,id2=10)
        def add(self):
            self.ans[self.i]=int(self.num1[self.i])+int(self.num2[self.i])              #run algorithms to get answer sheet
        def minu(self):
            self.ans[self.i]=int(self.num1[self.i])-int(self.num2[self.i])
        def mult(self):
            self.ans[self.i]=int(self.num1[self.i])*int(self.num2[self.i])
        def divi(self):
            self.ans[self.i]=self.a
        def teach_pass(self,event):
            self.copass=self.pasw.GetValue()
            print("y")
            if self.copass=="yml192300":
                print("y")
                panel = wx.Panel(parent=self)
                self.pasw.Destroy()
                self.pasw=wx.TextCtrl(panel,style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER,id=51)
                self.vs2.Add(self.pasw,proportion=-1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
        def on_click(self, event):
            source_id=event.GetId()
            print(source_id)
            if source_id==50:                                          #submit button
                self.score=0
                for self. i in range(len (self.ans)):                  #use loop corresponding to the amount of answer to check every question
                    if self.opp[self.i]=="+":
                        self.add()
                    if self.opp [self.i]=="-":
                        self.minu()
                    if self.opp[self.i]=="x":
                        self.mult()
                    if self.opp [self.i]=="/":
                        self.divi()
                print(self.ans)
                print(self.ansu)
                for i in range (len (self.ans)):
                    if float(self.ansu[i])==float(self.ans[i]):        #check user answer with true answer
                        self.score+=1
                print (self.score)
                self.score1.SetLabelText(str(self. score))
            if source_id==49:                                          # source id 49 = next
                self.num1=[0,0,0,0,0,0]
                self.num2=[0,0,0,0,0,0]
                self.opp=["-","-","-","-","-","-"]
                self.ans=[0,0,0,0,0,0]
                self.ansu=[0,0,0,0,0,0]
                self.oppr=int()
                self.score=int(0)
                for i in range (len (self.ans)):
                    self.ans[i]=0
                    self.num1[i]= str(random.randint(0,99))
                    self.num2[i]= str(random.randint(0,99))
                    self.oppr=random.randint(1,4)
                    if self.oppr == 1:                                 # CHANGE operation to random selected
                        self.opp[i]="+"
                    if self.oppr==2:
                        self.opp[i]=="-"
                    if self.oppr== 3:
                        self.opp[i]="x"
                    if self.oppr==4:
                        self.opp[i]="/"
                        a = int(random.randint(1,20))                  # Run division algorithm
                        b = int(random.randint(1,20))
                        c = int()
                        i = int(0)
                        c=a * b
                        self.num1[i]=c
                        self.num2[i]=b
                print(self.num1)
                print(self.num2)
                print(self.opp)
                self.test1_1.SetLabelText(str(self. num1[0]))
                self.test_opp1_1.SetLabelText(str(self. opp[0]))
                self.test1_2.SetLabelText(str(self.num2[0]))
    
                self.test2_1.SetLabelText(str(self. num1[1]))
                self.test_opp2_1.SetLabelText(str(self. opp[1]))
                self.test2_2.SetLabelText(str(self.num2[1]))
    
                self.test3_1.SetLabelText(str(self. num1[2]))
                self.test_opp3_1.SetLabelText(str(self. opp[2]))
                self.test3_2.SetLabelText(str(self.num2[2]))
    
                self.test4_1.SetLabelText(str(self. num1[3]))
                self.test_opp4_1.SetLabelText(str(self. opp[3]))
                self.test4_2.SetLabelText(str(self.num2[3]))
    
                self.test5_1.SetLabelText(str(self. num1[4]))
                self.test_opp5_1.SetLabelText(str(self. opp[4]))
                self.test5_2.SetLabelText(str(self.num2[4]))
    
                self.test6_1.SetLabelText(str(self. num1[5]))
                self.test_opp6_1.SetLabelText(str(self. opp[5]))
                self.test6_2.SetLabelText(str(self.num2[5]))
    
                self.ans1.Clear()
                self.ans2.Clear()
                self.ans3.Clear()
                self.ans4.Clear()
                self.ans5.Clear()
                self.ans6.Clear()
                        
        def Set_Ans(self, event):
            source_id=event.GetId()
            print(source_id)
            if source_id==1:
                self.ansu[source_id-1]=self.ans1.GetValue()
                print(self.ansu[source_id-1])
            if source_id==2:
                self.ansu[source_id-1]=self.ans2.GetValue()
                print(self.ans[source_id-1])
            if source_id==3:
                self.ansu[source_id-1]=self.ans3.GetValue()
                print(self.ansu[source_id-1])
            if source_id==4:
                self.ansu[source_id-1]=self.ans4.GetValue()
                print(self.ansu[source_id-1])
            if source_id==5:
                self.ansu[source_id-1]=self.ans5.GetValue()
                print(self.ansu[source_id-1])
            if source_id==6:
                self.ansu[source_id-1]=self.ans6.GetValue()
                print(self.ansu[source_id-1])
            
    class App(wx.App):
    
        def OnInit(self):
            frame=MyFrame()
            frame.Show()
            return True
        def OnExit(self):
            print('bye')
            return 0
    if __name__=='__main__':
        app=App()
        app.MainLoop()
    Last edited by gits; Dec 19 '19, 10:55 AM. Reason: added code tags
Working...