Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Python - wx.Frame GUI e thread
Forum - Python - wx.Frame GUI e thread

Avatar
arty.net (Normal User)
Newbie


Messaggi: 2
Iscritto: 14/05/2010

Segnala al moderatore
Postato alle 17:48
Giovedė, 20/05/2010
Ho un problema con questo codice :

Codice:
Codice sorgente - presumibilmente Python

  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4.  
  5. import random
  6. import wx
  7. import os                   # Operating System dependent call
  8. import sys                  
  9. import cPickle as p
  10. import csv
  11. import thread
  12.  
  13.  
  14. # Define dialog component
  15. ID_EXIT = 98
  16. ID_CALCULATE = 99
  17. ID_PLOT = 100
  18. ID_CLOSE = 101
  19. ID_CLEAR_TERM = 102
  20. ID_RESET = 103
  21. ID_CALCULATE_SINGLE = 104
  22.  
  23.  
  24. def formatta(valore):
  25.         tmp = str("%.1f" % valore)
  26.         if tmp == "-0.0":
  27.                 return tmp.replace("-","")
  28.         else:
  29.                 return tmp
  30.  
  31.  
  32. class MainWindow(wx.Frame):
  33.         def __init__(self, parent, id, title):
  34.                 wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(600, 600),pos = (200,100))
  35.        
  36.        
  37.                 #Creates a menu
  38.                 filemenu1=wx.Menu()
  39.                 filemenu1.AppendSeparator()
  40.                 filemenu1.Append(ID_EXIT, "&Exit", "Terminate the program")
  41.                 # Creating the menubar
  42.                 menuBar=wx.MenuBar()
  43.                 menuBar.Append(filemenu1, "&File") #Adding the filemenu to the MenuBar
  44.                 self.SetMenuBar(menuBar) #Adding the MenuBar to the Frame content
  45.                 #self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
  46.                 self.CreateStatusBar() # A statusbar in the bottom of the window
  47.        
  48.                 mod = ['B-ASK','4-ASK','8-ASK','BPSK','Q-PSK','8-PSK','Sunde FSK','QAM-16','QAM-64','QAM-256']
  49.                 incr = ['1.0', '0.5', '0.2','0.1']
  50.                
  51.                 wx.StaticText(self, -1, "Scegliere la modulazione :",(50,30))
  52.                 self.combo1 = wx.ComboBox(self, -1, pos=(50, 70), size=(150, -1), choices=mod, value='B-ASK',
  53.                                                 style=wx.CB_READONLY)
  54.                
  55.                 wx.StaticText(self, -1, "Scegliere i valori minimo e massimo di Eb/N0 (db):",(50,120))
  56.                 wx.StaticText(self, -1, "Minimo :",(50,175))
  57.                 wx.StaticText(self, -1, "Massimo :",(210,175))
  58.                 self.txt1 = wx.TextCtrl(self, -1, '0',(120,170))
  59.                
  60.                 self.txt2 = wx.TextCtrl(self, -1, '10',(290,170))
  61.                
  62.                 wx.StaticText(self, -1, "Scegliere l'incremento :",(50,240))
  63.                 self.combo2 = wx.ComboBox(self, -1, pos=(50, 280), size=(150, -1), choices=incr, value='1.0',
  64.                                                 style=wx.CB_READONLY)
  65.                
  66.                 #wx.Button(self, ID_CLOSE, 'Close', (500, 540))
  67.                 self.btn1 = wx.Button(self, ID_CALCULATE, 'Calculate', (50, 340))
  68.                 self.btn2 = wx.Button(self, ID_PLOT, 'Plot', (150, 340))
  69.                 self.btn3 = wx.Button(self, ID_RESET, 'Reset', (250, 340))
  70.                 self.btn4 = wx.Button(self, ID_CLEAR_TERM, 'Clear Terminal', (350, 340))
  71.                
  72.                 wx.StaticText(self, -1, "CALCOLO DI UN SOLO VALORE DEL BER :",(50,400))
  73.                 wx.StaticText(self, -1, "Inserire il valore di Eb/N0 (db) :",(50,430))
  74.                 wx.StaticText(self, -1, "Eb/N0 (db) :",(50,475))
  75.                 self.txt3 = wx.TextCtrl(self, -1, '0',(150,470))
  76.                 self.btn5 = wx.Button(self, ID_CALCULATE_SINGLE, 'Calculate single', (250, 465))
  77.                
  78.                 #self.rb2 = wx.RadioButton(self, -1, 'Off', (400, 395), style=wx.RB_GROUP)
  79.                 #self.rb1 = wx.RadioButton(self, -1, 'On', (350, 395),)
  80.                 #self.Bind(wx.EVT_RADIOBUTTON, self.disable_all, id=self.rb1.GetId())
  81.                 #self.Bind(wx.EVT_RADIOBUTTON, self.enable_all, id=self.rb2.GetId())
  82.                
  83.                 wx.EVT_MENU(self, ID_EXIT, self.OnClose)
  84.                
  85.                 wx.EVT_BUTTON(self, ID_CALCULATE, self.miothread1)
  86.                 #wx.EVT_BUTTON(self, ID_PLOT, self.miothread2)
  87.                 wx.EVT_BUTTON(self, ID_RESET, self.reset)
  88.                 wx.EVT_BUTTON(self, ID_CLEAR_TERM, self.clear_term)
  89.                 #wx.EVT_BUTTON(self, ID_CALCULATE_SINGLE, self.miothread3)
  90.                
  91.                 self.enable_all(True)
  92.                
  93.                 self.frame2 = Risultati(None,-1,"Risultati")
  94.                 self.frame2.Show(True)
  95.  
  96.  
  97.        
  98.         def disable_all(self,e):
  99.                 self.combo1.Disable()
  100.                 self.combo2.Enable()
  101.                 self.txt1.Enable()
  102.                 self.txt2.Enable()
  103.                 self.btn1.Disable()
  104.                 self.btn2.Disable()
  105.                 #self.btn3.Disable()
  106.                 #self.btn4.Disable()
  107.                
  108.                 self.txt3.Enable()
  109.                 self.btn5.Enable()
  110.        
  111.         def enable_all(self,e):
  112.                 self.combo1.Disable()
  113.                 self.combo2.Enable()
  114.                 self.txt1.Enable()
  115.                 self.txt2.Enable()
  116.                 self.btn1.Enable()
  117.                 self.btn2.Disable()
  118.                 self.btn3.Enable()
  119.                 #self.btn4.Enable()
  120.            
  121.                 self.txt3.Disable()
  122.                 self.btn5.Disable()
  123.    
  124.         def OnClose(self, e):
  125.                 self.Close(True)
  126.                 self.frame2.Close(True)
  127.        
  128.         #def calcolo(self,objMod,DeMod,titolo,minimum,maximum,incremento):
  129.         def calcolo(self,e):
  130.                 i = minimum = float(self.txt1.GetValue())
  131.                 maximum = float(self.txt2.GetValue())
  132.                 incremento = float(self.combo2.GetValue())
  133.            
  134.                
  135.                 print 'min value = '+str(float(minimum))
  136.                 print 'max value = '+str(float(maximum))
  137.                 print 'increase = '+str(incremento)
  138.                 print '---------------Results---------------'
  139.                
  140.                 self.frame2.log.AppendText('min value = '+str(float(minimum))+'\n'+'max value = '+str(float(maximum))+'\n'+'increase = '+str(incremento)+'\n'+'---------------Results---------------\n')
  141.                
  142.                 while True:            
  143.                         if i <= maximum:
  144.                        
  145.                                 self.frame2.LogMessage2(formatta(i))
  146.                                 print i            
  147.                                 i += incremento
  148.  
  149.                         else:
  150.                                 break
  151.                    
  152.                 #print 'Ber calculation done !'
  153.                 #self.frame2.LogMessage_single('Ber calculation done !')
  154.                 #self.frame2.log.AppendText('Ber caculation done !')
  155.                    
  156.    
  157.            
  158.         def clear_term(self,e):
  159.                 cmd = 'clear'
  160.                 os.system(cmd)
  161.                 self.frame2.log.Clear()
  162.            
  163.         def reset(self,e):
  164.                 os.system('rm ./sys/incremento.csv')
  165.                 os.system('rm ./sys/titolo.csv')
  166.                 os.system('rm ./risultati.csv')
  167.                 os.system('rm ./db.csv')
  168.                 #os.system('rm ./risultati.txt')
  169.                 #os.system('rm ./db.txt')
  170.                    
  171.         def miothread1(self,e):
  172.                 thread.start_new_thread(self.calcolo, (0,))
  173.        
  174.  
  175. class Risultati(wx.Frame):
  176.         def __init__(self, parent, id, title):
  177.                 wx.Frame.__init__(self, parent, id, title)
  178.                
  179.                 panel = wx.Panel(self)
  180.                 self.log = wx.TextCtrl(panel, -1, "",style=wx.TE_MULTILINE)
  181.                 self.log.SetEditable(False)
  182.                
  183.                 box = wx.BoxSizer(wx.VERTICAL)
  184.                 box.Add(self.log, 1, wx.EXPAND|wx.ALL, 5)
  185.                 #self.SetSizer(box)
  186.                 panel.SetSizer(box)
  187.  
  188.    
  189.         def LogMessage(self, msg1, msg2):
  190.                 self.log.AppendText(msg1+' : '+msg2+'\n')
  191.    
  192.         def LogMessage2(self, msg1):
  193.                 self.log.AppendText(msg1+'\n')
  194.        
  195.        
  196. #class MyApp(wx.App):
  197.     #def OnInit(self):
  198.         #frame=MainWindow(None, -1, "BER Tool AWGN")
  199.         #frame.Show(True)
  200.         ##frame2 = Risultati(None,-1,"Risultati")
  201.         ##frame2.Show(True)
  202.         #self.SetTopWindow(frame)
  203.         #return True
  204.        
  205. app = wx.App()
  206. frame=MainWindow(None, -1, "BER Tool AWGN")
  207. frame.Show(True)
  208. app.MainLoop()


E' la semplice stampa di un ciclo while su un secono frame chiamato Risultati, comandato dal frame principale. Premendo il tasto Calculate si avvia il ciclo e viene stampato sul TextCtrl del frame Risultati. Voglio inoltre che per il ciclo venga usato un altro thread rispetto a quello principale del programma. Ottengo molto spesso (nel 50% dei casi ) gli errori


Codice sorgente - presumibilmente Plain Text

  1. Segmentation Fault



oppure

Codice sorgente - presumibilmente Plain Text

  1. (python:11242): Gtk-CRITICAL **: gtk_text_layout_wrap_loop_start: assertion `layout->one_style_cache == NULL' failed
  2.  
  3. (python:11242): Gtk-CRITICAL **: gtk_text_layout_wrap_loop_end: assertion `layout->wrap_loop_count > 0' failed



come devo fare per evitare gli errori ? Non li fa sempre ma č proprio questo che devo risolvere, specialmente per innestare in seguito del codice + complesso....Girovagando in giro su internet ho visto che probabilmente č dovuto al fatto che sia il ciclo che l'AppendText sul frame Risultati sono governtati dallo stesso thread ( nuovo, che creo con la classe thread).

http://www.daniweb.com/forums/thread273160.html

E' possibile ? Esiste un modo per risolverlo ? grz in anticipo

PM Quote