Level: Beginner
I have been working with wxPython for a month now. I almost completely my GUI application, but when it came to threading I got stuck. I am using python v2.7 and wxPython v3.0 and the OS is windows 7.
My GUI app: Well in my GUI app I am reading some values from a server and then based upon number of these values I create panels in my GUI. Then each of these panels will represent the values in form of a staticText. For eg: If I receive from server 1,2,3 values, then I create 3 panels each displaying 1, 2 and 3 respectively. Till here everything works fine.
Problem: I want to check the server every 5 seconds to fetch the values and accordingly update my GUI ie. I have to add new panels to display the new values. I read some tutorials & posts on SC regarding using threads, I understood some basic facts too. Unfortunately I don't understand how to apply this concept to my problem. I think I have to put the code that checks the values from the server in threading loop. But I don't understand what else to do. I think I may have to change the logic of my code (creating panels part) for using threads. It would be really great if I could get a working example for my this particular problem so that I could apply the same to my rest of the application.
Code: I have created a short sample code for this particular problem. The getLabels() in the class labels mimics the server by just generating some random values and returning them in a list. Then these values are used by createPanels() to create panels and to display these values. It would be really great if some one can teach me how to use threading to add new panels to my GUI without freezing/blocking my GUI.
Download: The entire code can be found below and can also be downloaded from here to avoid any identation problem.
#!/usr/bin/env python
from random import randrange
import wx
import wx.lib.scrolledpanel
class GUI(wx.Frame):
def __init__(self, parent, id, title):
screenWidth = 800
screenHeight = 450
screenSize = (screenWidth, screenHeight)
wx.Frame.__init__(self, None, id, title, size=screenSize)
self.locationFont = locationFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = sizer = wx.BoxSizer(wx.VERTICAL)
self.panel = panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.SIMPLE_BORDER)
panel.SetupScrolling()
panel.SetBackgroundColour('#FFFFFF')
self.createPanels()
panel.SetSizer(sizer)
mainSizer.Add(panel, 15, wx.EXPAND|wx.ALL)
self.SetSizer(mainSizer)
def createPanels(self):
k = 0
labelObj = labels()
locations = labelObj.getLabel()
print locations
for i in locations:
sPanels = 'sPanel'+str(k)
sPanels = wx.Panel(self.panel)
label = str(k+1)
text = wx.StaticText(sPanels, -1, label)
text.SetFont(self.locationFont)
text.SetForegroundColour('#0101DF')
self.sizer.Add(sPanels, 0, wx.ALL, 5)
self.sizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 0)
k += 1
################################################
class labels():
def getLabel(self):
mylist =[]
i = randrange(10)
for k in range(1,i+1):
list.append(k)
return mylist
###############################################
if __name__=='__main__':
app = wx.App()
frame = GUI(parent=None, id=-1, title="Test")
frame.Show()
app.MainLoop()
If you don't see any panels after executing the code then please re execute the code.
Thank you for your time.