By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,583 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,583 IT Pros & Developers. It's quick & easy.

problem of switching panel in wxpython

P: 1
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.


Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import random
  3. class MyFrame(wx.Frame):
  4.     num1=[0,0,0,0,0,0]
  5.     num2=[0,0,0,0,0,0]
  6.     opp=["-","-","-","-","-","-"]
  7.     ans=[0,0,0,0,0,0]
  8.     ansu=[0,0,0,0,0,0]
  9.     oppr=int()
  10.     score=int(0)
  11.     for i in range (len (ans)):
  12.         ans[i]=0
  13.         oppr=random.randint(1,4)
  14.         num1[i]= str(random.randint(0,99))
  15.         num2[i]= str(random.randint(0,99))
  16.         if oppr == 1:
  17.             opp[i]="+"
  18.         if oppr==2:
  19.             opp[i]=="-"
  20.         if oppr== 3:
  21.             opp[i]="x"
  22.         if oppr==4:
  23.             opp[i]="/"
  24.             a = int(random.randint(1,20))
  25.             b = int(random.randint(1,20))
  26.             c = int()
  27.             c=a * b
  28.             print(c)
  29.             print(b)
  30.             print(a)
  31.             print(i)
  32.             num1[i]=str(c)
  33.             num2[i]=str(b)
  34.  
  35.  
  36.  
  37.     def __init__(self):
  38.         super().__init__(parent=None,title="new",size=(1000,1000),pos=(100,100))
  39.  
  40.  
  41.         self.paneln = wx.Panel(parent=self)
  42.         self.Refresh()
  43.         self.paneln.SetBackgroundColour(wx.GREEN)
  44.         vert=wx.BoxSizer(wx.VERTICAL)
  45.         self.math=[
  46.             wx.Bitmap("C:/Users/windows/Desktop/pi5oA6xKT.gif",wx.BITMAP_TYPE_GIF)
  47.             ]
  48.         self.image=wx.StaticBitmap(self.paneln,-1,self.math[0])
  49.         vert.Add(self.image,proportion=2,flag=wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
  50.         self.login=wx.Button(parent=self.paneln,id=101, label='login')
  51.         vert.Add(self.login,proportion=2,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.TOP|wx.BOTTOM|wx.CENTER,border=100)
  52.         self.login.SetBackgroundColour("PURPLE")
  53.  
  54.         self.paneln.SetSizer(vert)
  55.         self.Bind(wx.EVT_BUTTON,self.newf,id=101)
  56.  
  57.     def newf (self,event):
  58.  
  59.         self.paneln.Destroy()
  60.  
  61.         self.panel= wx.Panel(self)
  62.         self.panel.SetBackgroundColour(wx.GREEN)
  63.  
  64.         hs1=wx.BoxSizer()
  65.         vs=wx.BoxSizer(wx.VERTICAL)
  66.         self.vs2=wx.BoxSizer(wx.VERTICAL)
  67.         vs3=wx.BoxSizer(wx.VERTICAL)
  68.         hs2=wx.BoxSizer()
  69.         hs3=wx.BoxSizer()
  70.         hs4=wx.BoxSizer()
  71.         hs5=wx.BoxSizer()
  72.         hs6=wx.BoxSizer()
  73.         hs7=wx.BoxSizer()
  74.         hs8=wx.BoxSizer()
  75.         hsm=wx.BoxSizer()
  76.  
  77.  
  78.         self.title=wx.StaticText(parent=self.panel,label="Math Test")
  79.         vs.Add(self.title,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  80.         self.title2=wx.StaticText(parent=self.panel,label="practice test")
  81.         vs.Add(self.title2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  82. #############################################################################################
  83.         self.test1_1=wx.StaticText(parent=self.panel,label=self.num1[0])
  84.         hs1.Add(self.test1_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  85.         self.test_opp1_1=wx.StaticText(parent=self.panel,label=self.opp[0])
  86.         hs1.Add(self.test_opp1_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  87.         self.test1_2=wx.StaticText(parent=self.panel,label=self.num2[0])
  88.         hs1.Add(self.test1_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  89.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  90.         hs1.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  91.         self.ans1=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=1)
  92.         hs1.Add(self.ans1,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  93.         vs.Add(hs1,proportion=1,flag=wx.CENTER,)
  94. #--------------------------------------------------------------------------------------------------
  95.         self.test2_1=wx.StaticText(parent=self.panel,label=self.num1[1])
  96.         hs2.Add(self.test2_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  97.         self.test_opp2_1=wx.StaticText(parent=self.panel,label=self.opp[1])
  98.         hs2.Add(self.test_opp2_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  99.         self.test2_2=wx.StaticText(parent=self.panel,label=self.num2[1])
  100.         hs2.Add(self.test2_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  101.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  102.         hs2.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  103.         self.ans2=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=2)
  104.         hs2.Add(self.ans2,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  105.         vs.Add(hs2,proportion=1,flag=wx.CENTER,)
  106. #----------------------------------------------------------------------------------------------------
  107.         self.test3_1=wx.StaticText(parent=self.panel,label=self.num1[2])
  108.         hs3.Add(self.test3_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  109.         self.test_opp3_1=wx.StaticText(parent=self.panel,label=self.opp[2])
  110.         hs3.Add(self.test_opp3_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  111.         self.test3_2=wx.StaticText(parent=self.panel,label=self.num2[2])
  112.         hs3.Add(self.test3_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  113.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  114.         hs3.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  115.         self.ans3=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=3)
  116.         hs3.Add(self.ans3,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  117.         vs.Add(hs3,proportion=1,flag=wx.CENTER,)
  118. #-------------------------------------------------------------------------------------------------------
  119.         self.test4_1=wx.StaticText(parent=self.panel,label=self.num1[3])
  120.         hs4.Add(self.test4_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  121.         self.test_opp4_1=wx.StaticText(parent=self.panel,label=self.opp[3])
  122.         hs4.Add(self.test_opp4_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  123.         self.test4_2=wx.StaticText(parent=self.panel,label=self.num2[3])
  124.         hs4.Add(self.test4_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  125.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  126.         hs4.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  127.         self.ans4=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=4)
  128.         hs4.Add(self.ans4,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  129.         vs.Add(hs4,proportion=1,flag=wx.CENTER,)
  130. #-------------------------------------------------------------------------------------------------------
  131.         self.test5_1=wx.StaticText(parent=self.panel,label=self.num1[4])
  132.         hs5.Add(self.test5_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  133.         self.test_opp5_1=wx.StaticText(parent=self.panel,label=self.opp[4])
  134.         hs5.Add(self.test_opp5_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  135.         self.test5_2=wx.StaticText(parent=self.panel,label=self.num2[4])
  136.         hs5.Add(self.test5_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  137.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  138.         hs5.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  139.         self.ans5=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=5)
  140.         hs5.Add(self.ans5,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  141.         vs.Add(hs5,proportion=1,flag=wx.CENTER,)
  142. #-------------------------------------------------------------------------------------------------------
  143.         self.test6_1=wx.StaticText(parent=self.panel,label=self.num1[5])
  144.         hs6.Add(self.test6_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  145.         self.test_opp6_1=wx.StaticText(parent=self.panel,label=self.opp[5])
  146.         hs6.Add(self.test_opp6_1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  147.         self.test6_2=wx.StaticText(parent=self.panel,label=self.num2[5])
  148.         hs6.Add(self.test6_2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  149.         self.test_e=wx.StaticText(parent=self.panel,label="=")
  150.         hs6.Add(self.test_e,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  151.         self.ans6=wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER,id=6)
  152.         hs6.Add(self.ans6,proportion=1,flag=wx.FIXED_MINSIZE|wx.BOTTOM|wx.CENTER,border=10)
  153.         vs.Add(hs6,proportion=1,flag=wx.CENTER,)
  154. #######################################################################################################
  155.  
  156.         self.score1=wx.StaticText(parent=self.panel,label=str(self.score))
  157.         hs7.Add(self.score1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  158.         self.score2=wx.StaticText(parent=self.panel,label="/6")
  159.         hs7.Add(self.score2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  160.         vs.Add(hs7,proportion=1,flag=wx.CENTER,)
  161.         #-------------------------------------------------------------------------------
  162.         self.rep1=wx.StaticText(parent=self.panel,label=" [1] ")
  163.         hs8.Add(self.rep1,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  164.         self.rep2=wx.StaticText(parent=self.panel,label=" [2] ")
  165.         hs8.Add(self.rep2,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  166.         self.rep3=wx.StaticText(parent=self.panel,label=" [3] ")
  167.         hs8.Add(self.rep3,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  168.         self.rep4=wx.StaticText(parent=self.panel,label=" [4] ")
  169.         hs8.Add(self.rep4,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  170.         self.rep5=wx.StaticText(parent=self.panel,label=" [5] ")
  171.         hs8.Add(self.rep5,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  172.         self.rep6=wx.StaticText(parent=self.panel,label=" [6] ")
  173.         hs8.Add(self.rep6,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  174.         vs.Add(hs8,proportion=1,flag=wx.CENTER,)
  175. #########################################################################################################
  176.         self.submit=wx.Button(parent=self.panel,id=50, label='submit')
  177.         vs.Add(self.submit,proportion=2,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  178.         self.submit.SetBackgroundColour("PURPLE")
  179. #-------------------------------------------------------------------------------------------------------
  180.         self.next=wx.Button(parent=self.panel,id=49, label='next')
  181.         vs.Add(self.next,proportion=2,flag=wx.EXPAND|wx.BOTTOM|wx.CENTER,border=10)
  182.         self.next.SetBackgroundColour("PURPLE")
  183. ############################################################################################
  184.         self.teach=wx.StaticText(parent=self.panel,label="teacher section:")
  185.         self.vs2.Add(self.teach,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
  186.         self.teacher_gif=[
  187.             wx.Bitmap("C:/Users/windows/Desktop/image.png",wx.BITMAP_TYPE_PNG)
  188.             ]
  189.         self.image=wx.StaticBitmap(self.panel,-1,self.teacher_gif[0])
  190.         self.vs2.Add(self.image,proportion=5,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=10)
  191.         self.pas=wx.StaticText(parent=self.panel,label="teacher password:")
  192.         self.vs2.Add(self.pas,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
  193.         self.pasw=wx.TextCtrl(self.panel,style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER,id=51)
  194.         self.vs2.Add(self.pasw,proportion=1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
  195.         hsm.Add(vs,proportion=1,flag=wx.CENTER)
  196.         hsm.Add(self.vs2,proportion=1,flag=wx.CENTER)
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.         self.panel.SetSizer(hsm)
  204.         hsm.Layout()
  205.         self.Refresh()
  206.         self.Bind(wx.EVT_BUTTON,self.on_click,id=10,id2=50)
  207.         self.Bind(wx.EVT_TEXT_ENTER,self.teach_pass,id=51)
  208.         self.Bind(wx.EVT_TEXT,self.Set_Ans,id=0,id2=10)
  209.     def add(self):
  210.         self.ans[self.i]=int(self.num1[self.i])+int(self.num2[self.i])              #run algorithms to get answer sheet
  211.     def minu(self):
  212.         self.ans[self.i]=int(self.num1[self.i])-int(self.num2[self.i])
  213.     def mult(self):
  214.         self.ans[self.i]=int(self.num1[self.i])*int(self.num2[self.i])
  215.     def divi(self):
  216.         self.ans[self.i]=self.a
  217.     def teach_pass(self,event):
  218.         self.copass=self.pasw.GetValue()
  219.         print("y")
  220.         if self.copass=="yml192300":
  221.             print("y")
  222.             panel = wx.Panel(parent=self)
  223.             self.pasw.Destroy()
  224.             self.pasw=wx.TextCtrl(panel,style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER,id=51)
  225.             self.vs2.Add(self.pasw,proportion=-1,flag=wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM|wx.CENTER,border=50)
  226.     def on_click(self, event):
  227.         source_id=event.GetId()
  228.         print(source_id)
  229.         if source_id==50:                                          #submit button
  230.             self.score=0
  231.             for self. i in range(len (self.ans)):                  #use loop corresponding to the amount of answer to check every question
  232.                 if self.opp[self.i]=="+":
  233.                     self.add()
  234.                 if self.opp [self.i]=="-":
  235.                     self.minu()
  236.                 if self.opp[self.i]=="x":
  237.                     self.mult()
  238.                 if self.opp [self.i]=="/":
  239.                     self.divi()
  240.             print(self.ans)
  241.             print(self.ansu)
  242.             for i in range (len (self.ans)):
  243.                 if float(self.ansu[i])==float(self.ans[i]):        #check user answer with true answer
  244.                     self.score+=1
  245.             print (self.score)
  246.             self.score1.SetLabelText(str(self. score))
  247.         if source_id==49:                                          # source id 49 = next
  248.             self.num1=[0,0,0,0,0,0]
  249.             self.num2=[0,0,0,0,0,0]
  250.             self.opp=["-","-","-","-","-","-"]
  251.             self.ans=[0,0,0,0,0,0]
  252.             self.ansu=[0,0,0,0,0,0]
  253.             self.oppr=int()
  254.             self.score=int(0)
  255.             for i in range (len (self.ans)):
  256.                 self.ans[i]=0
  257.                 self.num1[i]= str(random.randint(0,99))
  258.                 self.num2[i]= str(random.randint(0,99))
  259.                 self.oppr=random.randint(1,4)
  260.                 if self.oppr == 1:                                 # CHANGE operation to random selected
  261.                     self.opp[i]="+"
  262.                 if self.oppr==2:
  263.                     self.opp[i]=="-"
  264.                 if self.oppr== 3:
  265.                     self.opp[i]="x"
  266.                 if self.oppr==4:
  267.                     self.opp[i]="/"
  268.                     a = int(random.randint(1,20))                  # Run division algorithm
  269.                     b = int(random.randint(1,20))
  270.                     c = int()
  271.                     i = int(0)
  272.                     c=a * b
  273.                     self.num1[i]=c
  274.                     self.num2[i]=b
  275.             print(self.num1)
  276.             print(self.num2)
  277.             print(self.opp)
  278.             self.test1_1.SetLabelText(str(self. num1[0]))
  279.             self.test_opp1_1.SetLabelText(str(self. opp[0]))
  280.             self.test1_2.SetLabelText(str(self.num2[0]))
  281.  
  282.             self.test2_1.SetLabelText(str(self. num1[1]))
  283.             self.test_opp2_1.SetLabelText(str(self. opp[1]))
  284.             self.test2_2.SetLabelText(str(self.num2[1]))
  285.  
  286.             self.test3_1.SetLabelText(str(self. num1[2]))
  287.             self.test_opp3_1.SetLabelText(str(self. opp[2]))
  288.             self.test3_2.SetLabelText(str(self.num2[2]))
  289.  
  290.             self.test4_1.SetLabelText(str(self. num1[3]))
  291.             self.test_opp4_1.SetLabelText(str(self. opp[3]))
  292.             self.test4_2.SetLabelText(str(self.num2[3]))
  293.  
  294.             self.test5_1.SetLabelText(str(self. num1[4]))
  295.             self.test_opp5_1.SetLabelText(str(self. opp[4]))
  296.             self.test5_2.SetLabelText(str(self.num2[4]))
  297.  
  298.             self.test6_1.SetLabelText(str(self. num1[5]))
  299.             self.test_opp6_1.SetLabelText(str(self. opp[5]))
  300.             self.test6_2.SetLabelText(str(self.num2[5]))
  301.  
  302.             self.ans1.Clear()
  303.             self.ans2.Clear()
  304.             self.ans3.Clear()
  305.             self.ans4.Clear()
  306.             self.ans5.Clear()
  307.             self.ans6.Clear()
  308.  
  309.     def Set_Ans(self, event):
  310.         source_id=event.GetId()
  311.         print(source_id)
  312.         if source_id==1:
  313.             self.ansu[source_id-1]=self.ans1.GetValue()
  314.             print(self.ansu[source_id-1])
  315.         if source_id==2:
  316.             self.ansu[source_id-1]=self.ans2.GetValue()
  317.             print(self.ans[source_id-1])
  318.         if source_id==3:
  319.             self.ansu[source_id-1]=self.ans3.GetValue()
  320.             print(self.ansu[source_id-1])
  321.         if source_id==4:
  322.             self.ansu[source_id-1]=self.ans4.GetValue()
  323.             print(self.ansu[source_id-1])
  324.         if source_id==5:
  325.             self.ansu[source_id-1]=self.ans5.GetValue()
  326.             print(self.ansu[source_id-1])
  327.         if source_id==6:
  328.             self.ansu[source_id-1]=self.ans6.GetValue()
  329.             print(self.ansu[source_id-1])
  330.  
  331. class App(wx.App):
  332.  
  333.     def OnInit(self):
  334.         frame=MyFrame()
  335.         frame.Show()
  336.         return True
  337.     def OnExit(self):
  338.         print('bye')
  339.         return 0
  340. if __name__=='__main__':
  341.     app=App()
  342.     app.MainLoop()
Dec 19 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.