473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

My widgets are having trouble communicating.

Jory R Ferrell
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
8 1552
bvdet
2,851 Expert Mod 2GB
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
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
126 100+
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
Any idea why the second label won't update even before I start the loop?
Feb 3 '12 #5
Smygis
126 100+
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
Alright...btw....whats with your avatar? It seems...'odd'. :/
Feb 3 '12 #7
Smygis
126 100+
The base silhouette was the default avatar for new users on TheScripts(.com). I just 'personalized' it a little bit.
Feb 3 '12 #8
Any idea why my second label isn't updating before I activate the loop though?
Feb 4 '12 #9

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

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
2
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2)....
1
by: Lauren Wilson | last post by:
I'm having trouble with the Access VBA help on my installation of A2K with Dev tools. Every time I try to retrieve help for items listed in the Object Browser (and SOME other items as well),...
2
by: Jozef | last post by:
Hello, I am trying to put together a module and open a workspace on a database that has a simple password (using Access XP). This is the lin that I'm having trouble with; Set wrk =...
0
by: Jozef | last post by:
Hello, I'm having trouble with the download links on my web server. The error I'm getting is; CGI Timeout The specified CGI application exceeded the allowed time for processing. The server...
1
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted...
1
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() ...
2
by: Jake Barnes | last post by:
I've read over the documentation for these effects: http://wiki.script.aculo.us/scriptaculous/show/CombinationEffectsDemo I want to include them on my page. I tried attaching using onload, but...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
2
by: Stu | last post by:
Hi guys, I've been having trouble getting the clock function to work portably, please could I get some thoughts? <Possibly OT comments> It works fine on my laptop (under WinXP) and on my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.