[pygtk] Threading Question
Got some help last week in regard to a problem which put me on the trail of understanding threading. Through reading and playing around I've this working example. What I don't understand are how certain parts of it work. BTW, I'm working on a windows PC. import threading import random, time import gtk #Initializing the gtk's thread engine gtk.gdk.threads_init() class FractionSetter(threading.Thread): """This class sets the fraction of the progressbar""" def run(self): progressbar.set_fraction(random.random()) print 'first' time.sleep(2) progressbar.set_fraction(random.random()) print 'second' time.sleep(2) gtk.main_quit() def stop(self): """Stop method, sets the event to terminate the thread's main loop""" self.stopthread.set() def main_quit(obj): fs.stop() gtk.main_quit() #Gui bootstrap: window and progressbar window = gtk.Window() progressbar = gtk.ProgressBar() window.add(progressbar) window.show_all() window.connect('destroy', main_quit) #Creating and starting the thread fs = FractionSetter() fs.start() gtk.gdk.threads_enter() gtk.main() gtk.gdk.threads_leave() My question is: a) What does putting gtk.main() between gtk.gdk.threads _enter & gtk.gdk.threads _leave actually do? b) Am I right in thinking the time.sleep(2) are there so that the progressbar is changed and the gui window is updated? c) Without the time.sleep(2) I'm thinking the progressbar would be changed but the gui window wouldn't be updated. Correct? Still getting my head around threads. See all sorts of references all of which seem a wee bit beyond my comprehension at the moment. Any comments would be appreciated. Thanks Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Updating a window
Appreciate the time you've taken to respond. Yes, I'm new to all of this. Had toyed with the thread idea but wanted learned thoughts from people the time BEFORE I committed the time to studying the various options. I'll certainly read what you referenced. Your response made good sense. Thanks so much. Brian Rowlands -Original Message- From: A.T.Hofkamp [mailto:a.t.hofk...@tue.nl] Sent: Wednesday, 23 June 2010 9:19 p.m. To: Brian Rowlands (Greymouth High School) Cc: Michael Urman; pygtk@daa.com.au Subject: Re: [pygtk] Updating a window Brian Rowlands (Greymouth High School) wrote: > Thought I'd put together a small example to show what I mean: > > import pygtk > pygtk.require("2.0") > import gtk > import sys > import time > > def do_stuff(): > time.sleep(5) > print 'brian' > > # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> handles > > class handles: > def on_login_window_destroy(event): > sys.exit(1) > > > # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> define main > > class LoginApp: > def __init__(self): > # set the glade file > self.builder = gtk.Builder() > self.builder.add_from_file("login.glade") > self.window = self.builder.get_object("login_window") > self.builder.connect_signals(handles.__dict__) > self.server = self.builder.get_object("server") > self.info = self.builder.get_object("info") > self.window.show() > > > def __getitem__(self,key): > # provide link to widgets > return self.builder.get_object(key) > > > # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run application > > if __name__ == '__main__': > ghs = LoginApp() > do_stuff() > gtk.main() There is only one thread of control, so after constructing the LoginApp() object, you block the *whole* program for 5 seconds by sleeping, print your name, and *then* start event handling of the window. The latter does drawing, and rereshing. That is why the program behaves as it does. > > The window outline appears but no inside until after 5 sec when the full > window takes shape my name then appears. > > What I want is the full window appearing at the beginning and then 5 sec > later my name appears. > > Make sense? What you want is clear to all, but the way you want it, is not going to fly. You cannot block in the thread of control that does 'gtk.main()' (well, you can, but it does not do what you want). There are 2 solutions to this: 1. start another thread to do your processing, and have it coummunicate with the gtk.main() thread to update the window. 2. do everything event-based. Instead of sleeping, start a timer, and attach 'print name' to the handler that gets called when it times out. I would recommend that you look at the tutorial (http://pygtk.org/pygtk2tutorial/index.html), in particular chappters 19, 20, and 24. The latter is an example. To understand those chapters you may want to do the entire tutorial. Since you seem new to event-based programming, it won't be wasted time, I think. Albert ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Updating a window
Thought I'd put together a small example to show what I mean: import pygtk pygtk.require("2.0") import gtk import sys import time def do_stuff(): time.sleep(5) print 'brian' # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> handles class handles: def on_login_window_destroy(event): sys.exit(1) # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> define main class LoginApp: def __init__(self): # set the glade file self.builder = gtk.Builder() self.builder.add_from_file("login.glade") self.window = self.builder.get_object("login_window") self.builder.connect_signals(handles.__dict__) self.server = self.builder.get_object("server") self.info = self.builder.get_object("info") self.window.show() def __getitem__(self,key): # provide link to widgets return self.builder.get_object(key) # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run application if __name__ == '__main__': ghs = LoginApp() do_stuff() gtk.main() The window outline appears but no inside until after 5 sec when the full window takes shape my name then appears. What I want is the full window appearing at the beginning and then 5 sec later my name appears. Make sense? Brian -Original Message- From: Michael Urman [mailto:mur...@gmail.com] Sent: Wednesday, 23 June 2010 12:46 p.m. To: Brian Rowlands (Greymouth High School) Cc: pygtk@daa.com.au Subject: Re: [pygtk] Updating a window On Tue, Jun 22, 2010 at 18:33, Brian Rowlands (Greymouth High School) wrote: > The issue I have is that the outline of the window appears with the inside > blank until do_stuff() completes when the window is fully defined. > > If I have: > > gtk.main() > do_stuff() > > then the window appears fine but nothing gets done. > > Don’t know if there is a command to update a window. > > Probably simple. If not, a reference/pointer would be appreciated. The right answer really depends on what sort of things you are doing inside do_stuff. If it's work that makes sense to move piecewise to signal handlers, then do that. If it's a batch of work with convenient locations to do event processing (say every time through a loop), call gtk.main_iteration() from time to time, possibly within a while gtk.events_pending(). Welcome to event-based programming! -- Michael Urman ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Updating a window
Thanks for the suggestion but it doesn't make any difference. Thanks for replying. Window only appears after the contents of do_stuff() have completed their tasks Do_stuff completes tasks like: * copy DB to c: drive from server * read DB * check user is allowed to log in * impersonate [ using Active Directory in a Win32 environment ] * etc Sorry. Brian From: cmni...@gmail.com [mailto:cmni...@gmail.com] On Behalf Of muhamed niyas Sent: Wednesday, 23 June 2010 4:11 p.m. To: Brian Rowlands (Greymouth High School) Subject: Updating a window Hi... Can u re-arrange the statement ghs.window.show() below the do_stuff() I mean the code like this if __name__ == '__main__': ghs = LoginApp() ghs['user'].set_text(user) ghs['pc'].set_text(pc_name) set_up_login_window() do_stuff() ghs.window.show() gtk.main() Pls try this and let me know the status... -- Thanks & Best Regards, Muhamed Niyas C (NuCore Software Solutions Pvt Ltd) Mobile: +91 9447 468825 URL: www.nucoreindia.com Email: ni...@nucoreindia.com ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Updating a window
Thanks for that. Do_stuff consists of things like: Impersonate a user Copy Db to c: drive Read sql DB and obtain PC settings Log-off certain types of users Change the registry Set printers inc default printer Write to a log file Etc So event handlers doesn't seem to fix the bill in my mind. Roughly speaking, the GUI window displays a message to the user. Periodically, this changes as the login script runs through it's tasks. Even displays a goodbye message on the occasions the user shouldn't be allowed to log in. When I used Perl, there was a window update function. Alas, I'm unsure of where to go now. BTW, wrote GUI window in Glade if that makes any difference. Would appreciate feedback Thanks -Original Message- From: Michael Urman [mailto:mur...@gmail.com] Sent: Wednesday, 23 June 2010 12:46 p.m. To: Brian Rowlands (Greymouth High School) Cc: pygtk@daa.com.au Subject: Re: [pygtk] Updating a window On Tue, Jun 22, 2010 at 18:33, Brian Rowlands (Greymouth High School) wrote: > The issue I have is that the outline of the window appears with the inside > blank until do_stuff() completes when the window is fully defined. > > If I have: > > gtk.main() > do_stuff() > > then the window appears fine but nothing gets done. > > Don’t know if there is a command to update a window. > > Probably simple. If not, a reference/pointer would be appreciated. The right answer really depends on what sort of things you are doing inside do_stuff. If it's work that makes sense to move piecewise to signal handlers, then do that. If it's a batch of work with convenient locations to do event processing (say every time through a loop), call gtk.main_iteration() from time to time, possibly within a while gtk.events_pending(). Welcome to event-based programming! -- Michael Urman ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
[pygtk] Updating a window
Novice but eager to learn. I'm writing a login script which displays a GUI window and which I want to update with comments as the script runs. My script finishes with: if __name__ == '__main__': ghs = LoginApp() ghs['user'].set_text(user) ghs['pc'].set_text(pc_name) set_up_login_window() ghs.window.show() do_stuff() gtk.main() The issue I have is that the outline of the window appears with the inside blank until do_stuff() completes when the window is fully defined. If I have: .. gtk.main() do_stuff() then the window appears fine but nothing gets done. Don't know if there is a command to update a window. Probably simple. If not, a reference/pointer would be appreciated. Many Thanks ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Win 32 query
Hi Folks After searching for the best part of an hour AND posting a cry for help via this list I immediately found what I wanted via Google. dc = win32net.NetGetDCName (None, None) produces what I want. Question: When should I really give up and ask for help? H Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
[pygtk] Win 32 query
Hi Folks Hope you can help me with a simple problem. I have a login script that requires me to display the username and domain controller the user connects through when logging in to Active Directory. I can get their username via: from win32api import GetUserName user = GetUserName() My question is how do I get the domain controller they authenticated through? Hope you can help. Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Linking textview and combobox
Hi Folks Found my mental block: divisions = [] textbuffer = cbc['divisions'].get_buffer() text = cbc['divisions'].get_buffer().get_text(*textbuffer.get_bounds()) divisions = text.splitlines() <-- needed this and a minor change in the lines above if no_divisions != '': #set divisions list store = gtk.ListStore(str) for i in divisions: if i != '' : store.append([i]) cbc['divisionCB'].set_model(store) cell = gtk.CellRendererText() cbc['divisionCB'].pack_start(cell, True) cbc['divisionCB'].add_attribute(cell, 'text', 0) Cheers Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
[pygtk] Linking textview and combobox
Hi folks I'm trying to populate a combobox with the lines showing in a textview widget. My question is how do I do this? I've a textview widget called 'divisions'. This coding reads its associated buffer: textbuffer = cbc['divisions'].get_buffer() divisions = cbc['divisions'].get_buffer().get_text(*textbuffer.get_bounds()) If the lines in textview are: First line Second line Then divisions contains all these characters including \n. This coding creates one character entries in the combobox called divisonsCB: store = gtk.ListStore(str) for i in divisions: if i != '\n' : store.append(i) cbc['divisionCB'].set_model(store) cell = gtk.CellRendererText() cbc['divisionCB'].pack_start(cell, True) cbc['divisionCB'].add_attribute(cell, 'text', 0) Can someone help me think logically on this issue please? Thanks Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
Re: [pygtk] Which is the active radiobutton?
Thanks for all the good advice. Found my mental block. Solution: radio = [r for r in cbc['radio_ped'].get_group() if r.get_active()][0] radiolabel = radio.get_label() where radio_ped is the group name of the radio buttons Brian ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
[pygtk] Which is the active radiobutton?
Hi Guys Just a newbie using Python & Glade and have the code below: some code ... class BowlsApp: def __init__(self): # set the glade file self.builder = gtk.Builder() self.builder.add_from_file("Bowls.glade") self.window = self.builder.get_object("tournament") self.builder.connect_signals(handles.__dict__) def __getitem__(self,key): # provide link to widgets return self.builder.get_object(key) # >> run application if __name__ == '__main__': cbc = BowlsApp() cbc.window.show() gtk.main() The GUI contains 4 radio buttons [ I'll call them four, three, two, one ] in a group called four. My research came across: radio = [r for r in cbc['four'].get_group() if r.get_active()] which gets me the active button with print radio giving me: [] My question: how do i get the 'name' of the button which is active? Tried print radio.get_label() and that failed as there is no attribute called label. Any help appreciated. Even a reference. Thanks ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
[pygtk] Newbie question
Hi Guys I'm very, very new to both Python and Glade and I'm not even sure if I should be posting to this list. Anyway, here goes as I need help - even a useful reference. I'm working on a Win32 platform and have begun to create a python program as follows: #!/usr/bin/env python import pygtk pygtk.require("2.0") import gtk import sys #>>> handles class handles: def __init__(self): self.gladefile = "Bowls.glade" def on_quit_clicked(event): sys.exit(1) def on_tournament_destroy(event): sys.exit(1) def on_show_settings_clicked(event): pass #>>> define main class EventApp: def __init__(self): # set the glade file builder = gtk.Builder() builder.add_from_file("Bowls.glade") self.window = builder.get_object("tournament") builder.connect_signals(handles.__dict__) #>>> run application if __name__ == '__main__': w = EventApp() w.window.show() gtk.main() My simple question is: how do I reference a textfield [ example 'no_teams' ] when the button 'on_show_settings' is clicked. Say, print it's contents. Any help greatly appreciated. ___ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/