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

My widgets are having trouble communicating.

Jory R Ferrell
P: 62
Everytime I run the mainloop, it starts off fine. After pressing the Action button, the program freezes. The second label also fails to change.
Anyone else getting this problem on their comp or is it the way I wrote the code?

Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import time
  3.  
  4.  
  5. class LeftPanel(wx.Panel):
  6.     def __init__(self, parent, id):
  7.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  8.         self.text = parent.GetParent().rightPanel.text
  9.         self.text_2 = parent.GetParent().rightPanel.text_2
  10.         button1 = wx.Button(self, -1, 'Count', (10, 10))
  11.         button2 = wx.Button(self, -1, 'Countdown', (10, 60))
  12.         button3 = wx.Button(self, -1, 'Action', (10, 110))
  13.         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
  14.         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
  15.         self.Bind(wx.EVT_BUTTON, self.button_Pressed, id=button3.GetId())
  16.         self.timed_Out = 1     
  17.  
  18.  
  19.     def OnPlus(self, event):
  20.         value = 1
  21.         for t in range(5000):
  22.             value = value + 1
  23.             time.sleep(1)
  24.             self.text.SetLabel(str(value))
  25.  
  26.     def OnMinus(self, event):
  27.         import math
  28.         value = 60
  29.         for t in range(value):
  30.             value = value - 1
  31.             time.sleep(1)
  32.             self.text.SetLabel(str(value/60) + ':' + str(value%60))
  33.  
  34.         self.timed_Out = 0
  35.         self.text_2.SetLabel(str('End o\'line.'))
  36.  
  37.     def button_Pressed(self, event):
  38.         if self.timed_Out == 1:
  39.             if self.text_2 == 'First':
  40.                 self.text_2.SetLabel('Second')
  41.  
  42.             elif self.text_2 == 'Second':
  43.                  self.text_2.SetLabel('First')
  44.  
  45.  
  46.  
  47. class RightPanel(wx.Panel):
  48.     def __init__(self, parent, id):
  49.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  50.         self.text = wx.StaticText(self, -1, '0', (10,60))
  51.         self.text_2 = wx.StaticText(self,-1,'First',(10, 120))
  52.  
  53. class Communicate(wx.Frame):
  54.     def __init__(self, parent, id, title):
  55.         wx.Frame.__init__(self, parent, id, title, size=(600, 200))
  56.         panel = wx.Panel(self, -1)
  57.         self.rightPanel = RightPanel(panel, -1)
  58.         leftPanel = LeftPanel(panel, -1)
  59.         hbox = wx.BoxSizer()
  60.         hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 4)
  61.         hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
  62.         panel.SetSizer(hbox)
  63.         self.Centre()
  64.         self.Show(True)
  65.  
  66.  
  67.  
  68. app = wx.App()
  69. Communicate(None, -1, 'widgets communicate')
  70. app.MainLoop()
  71.  
  72.  
Feb 2 '12 #1
Share this Question
Share on Google+
8 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
If you were in Tkinter, I think I could help you. It may be a problem with time.sleep(). I am guessing - the GUI may not be able to respond to events after time.sleep() is called. Tkinter has a callback method after() which would be appropriate in your example. I think wxPython should have a similar method.
Feb 2 '12 #2

Jory R Ferrell
P: 62
Even before I activate the sleep function I can't change the second label using the Action button. Any ideas?
Feb 2 '12 #3

Smygis
100+
P: 126
Now I haven't touched programming in about 3 years so I'm really rusty and cant make much sense of you program. But way back when I wrote a small PLC interpreter and had similar issues with the interface that I wrote in wx.

And then I learned about the wonder that is wx.CallAfter() and everything just worked after that pretty much. Don't remember how it worked though. This was 5 years ago.

The more i try to figure out what i have forgotten the more I'm thinking that I'm not really helping here
Feb 2 '12 #4

Jory R Ferrell
P: 62
Any idea why the second label won't update even before I start the loop?
Feb 3 '12 #5

Smygis
100+
P: 126
I have been going through my old app and figuring out how i made it work and this is how i think i did it:

I have my "compiler/runtime" for PLC code run in a separate thread, i then had a second thread that using wx.CallAfter called functions that checked the state of variables in the runtime thread and updated the UI accordingly.

This could work for you application as well but needs some reprogramming and i guess is a bit convoluted. the main problem is as bvdet says that time.sleep locks the UI. I spent some time diggin in wx docs and managed to fix it a bit using wx.Yield:
Expand|Select|Wrap|Line Numbers
  1. import wx
  2. import time
  3.  
  4.  
  5. class LeftPanel(wx.Panel):
  6.     def __init__(self, parent, id):
  7.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  8.         self.text = parent.GetParent().rightPanel.text
  9.         self.text_2 = parent.GetParent().rightPanel.text_2
  10.         button1 = wx.Button(self, -1, 'Count', (10, 10))
  11.         button2 = wx.Button(self, -1, 'Countdown', (10, 60))
  12.         button3 = wx.Button(self, -1, 'Action', (10, 110))
  13.         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
  14.         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
  15.         self.Bind(wx.EVT_BUTTON, self.button_Pressed, id=button3.GetId())
  16.         self.timed_Out = 1     
  17.  
  18.  
  19.     def OnPlus(self, event):
  20.         value = 1
  21.         for t in range(50):
  22.             value = value + 1
  23.             time.sleep(1)
  24.             wx.Yield()
  25.             self.text.SetLabel(str(value))
  26.  
  27.     def OnMinus(self, event):
  28.  
  29.         value = 60
  30.         for t in range(value):
  31.             value = value - 1
  32.             time.sleep(1)
  33.             wx.Yield()
  34.             self.text.SetLabel(str(value/60) + ':' + str(value%60))
  35.  
  36.         self.timed_Out = 0
  37.         self.text_2.SetLabel(str('End o\'line.'))
  38.  
  39.     def button_Pressed(self, event):
  40.         if self.timed_Out == 1:
  41.             if self.text_2 == 'First':
  42.                 self.text_2.SetLabel('Second')
  43.  
  44.             elif self.text_2 == 'Second':
  45.                  self.text_2.SetLabel('First')
  46.  
  47.  
  48. class RightPanel(wx.Panel):
  49.     def __init__(self, parent, id):
  50.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  51.         self.text = wx.StaticText(self, -1, '0', (10,60))
  52.         self.text_2 = wx.StaticText(self,-1,'First',(10, 120))
  53.  
  54. class Communicate(wx.Frame):
  55.     def __init__(self, parent, id, title):
  56.         wx.Frame.__init__(self, parent, id, title, size=(600, 200))
  57.         panel = wx.Panel(self, -1)
  58.         self.rightPanel = RightPanel(panel, -1)
  59.         leftPanel = LeftPanel(panel, -1)
  60.         hbox = wx.BoxSizer()
  61.         hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 4)
  62.         hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
  63.         panel.SetSizer(hbox)
  64.         self.Centre()
  65.         self.Show(True)
  66.  
  67.  
  68.  
  69. app = wx.App()
  70. Communicate(None, -1, 'widgets communicate')
  71. app.MainLoop()
After that I'm not sure what happens. With this the application throws errors if you try clicking the other button while countdown/up is ongoing.

Buy hey its progress right.
Feb 3 '12 #6

Jory R Ferrell
P: 62
Alright...btw....whats with your avatar? It seems...'odd'. :/
Feb 3 '12 #7

Smygis
100+
P: 126
The base silhouette was the default avatar for new users on TheScripts(.com). I just 'personalized' it a little bit.
Feb 3 '12 #8

Jory R Ferrell
P: 62
Any idea why my second label isn't updating before I activate the loop though?
Feb 4 '12 #9

Post your reply

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