| | |
(k)dialog or zenity implemented as a python function
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
I don't know what to do with this xlib error I get from the code below (please paste it into the python console and see it for yourself).
I got some advice from some guy, but I am a newbie to wxpython so I don't know how to follow it and it will take a long time until I will become capable to do it. Since it may be much easier for you to do it, please somebody modify my code for me. This will be a much faster way for me to learn about threads and wxpython, too.
Note that this is just a simplified sample of my code, so don't try to change its design *completely* (just because this sample looks stupid and "useless anyway"). Using the same wxpython function in separate threads is vital. Think of it as a function that acts as a python implementation of zenity or (k)dialog. I just need to call this "(k)dialog"-like window function from many functions in many separate threads. However, no problem if I will need some extra code (i.e. outside the function) in the main body of the script. I just want as much as possible of the wx to be confined to a function.
This helps me keep the GUI and the code as separated as possible in my script. I am a console guy and prefer to write "scripts (sometimes with some GUI features)", not gripts.
#!/bin/python
import wx, threading; from wx.html import HtmlWindow
def wxwin():
class HtmlDialog(wx.MessageDialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.htmlwindow = HtmlWindow(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.clbutton = wx.Button(self, wx.ID_CLOSE)
self.sizer.Add(self.htmlwindow)
self.sizer.Add(self.clbutton)
self.htmlwindow.SetPage('message')
self.Bind(wx.EVT_BUTTON, self.function, self.clbutton)
def function(self, event): self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
dlg = HtmlDialog(None)
print str(dlg.ShowModal())
def thrd(): wxwin()
while True: wxwin(); threading.Thread(target=thrd).start()
THIS IS THE HELP I GOT (may be useful for you too):
you can't do wx things from more than one thread (except for things like wx.PostEvent or wx.CallAfter) and you can't have more than one wx.App object at a time.
ok, have only one thread for the UI that initializes wx and starts the MainLoop. In the other threads when you need to call a dialog function break it into two parts, one that just sends a message to the UI thread and waits for the response, and the other to catch the message in the UI thread that creates and shows the dialog, calls ShowModal and then send a message back to the waiting thread with the results.
I think I should use queue and PostEvent, but I don't know how.
I got some advice from some guy, but I am a newbie to wxpython so I don't know how to follow it and it will take a long time until I will become capable to do it. Since it may be much easier for you to do it, please somebody modify my code for me. This will be a much faster way for me to learn about threads and wxpython, too.
Note that this is just a simplified sample of my code, so don't try to change its design *completely* (just because this sample looks stupid and "useless anyway"). Using the same wxpython function in separate threads is vital. Think of it as a function that acts as a python implementation of zenity or (k)dialog. I just need to call this "(k)dialog"-like window function from many functions in many separate threads. However, no problem if I will need some extra code (i.e. outside the function) in the main body of the script. I just want as much as possible of the wx to be confined to a function.
This helps me keep the GUI and the code as separated as possible in my script. I am a console guy and prefer to write "scripts (sometimes with some GUI features)", not gripts.
#!/bin/python
import wx, threading; from wx.html import HtmlWindow
def wxwin():
class HtmlDialog(wx.MessageDialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.htmlwindow = HtmlWindow(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.clbutton = wx.Button(self, wx.ID_CLOSE)
self.sizer.Add(self.htmlwindow)
self.sizer.Add(self.clbutton)
self.htmlwindow.SetPage('message')
self.Bind(wx.EVT_BUTTON, self.function, self.clbutton)
def function(self, event): self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
dlg = HtmlDialog(None)
print str(dlg.ShowModal())
def thrd(): wxwin()
while True: wxwin(); threading.Thread(target=thrd).start()
THIS IS THE HELP I GOT (may be useful for you too):
you can't do wx things from more than one thread (except for things like wx.PostEvent or wx.CallAfter) and you can't have more than one wx.App object at a time.
ok, have only one thread for the UI that initializes wx and starts the MainLoop. In the other threads when you need to call a dialog function break it into two parts, one that just sends a message to the UI thread and waits for the response, and the other to catch the message in the UI thread that creates and shows the dialog, calls ShowModal and then send a message back to the waiting thread with the results.
I think I should use queue and PostEvent, but I don't know how.
![]() |
Other Threads in the Python Forum
- Previous Thread: Simple graphics in python assignment
- Next Thread: help.plz plz plz
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib





