Python Forum
[PyGUI] Timer Implementation with gtk-GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Timer Implementation with gtk-GUI
#1
Hi,
I am new to Python, I was active in tcl/tk for many years. I am trying to implement a simple GUI in combination with an asynchronic process running in parallel. Using linux as OS for my development env.

I can start the timer with activation of a button, but once it is activated, the GUI is blocked, the button is hold pressed and one can not interact with the GUI. I don't want it to be blocked, I want the GUI to stay "alive" while running the timed process. In this case, the process is executing the external 'netstat' program every 10 seconds once activated by the button.

Here the code:
#!/usr/bin/env python

# For the GUI
import pygtk
pygtk.require('2.0')
import gtk

# use sched to timing 
import time 
import os 
import sched 
 
#  Initialization sched Modular scheduler class
#  The first parameter is a function that returns the timestamp
#  The second parameter can be blocked before the timing arrives
schedule = sched.scheduler(time.time, time.sleep) 
 
#  A function that is triggered by periodic scheduling  
def execute_command(cmd, inc): 
	os.system(cmd) 
	schedule.enter(inc, 0, execute_command, (cmd, inc)) 

class Buttons:
    # Our usual callback method
    def callback(self, widget, data=None):
        print "%s" % data
        schedule.run() 

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Timer")
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())
        self.window.set_border_width(10)
        self.window.move(500,500)
        self.window.set_size_request(200, 200)

        # Create a new button
        button1 = gtk.Button("Enable Timer")
        button1.connect("clicked", self.callback)
        button1.show()
        self.window.add(button1)
        
        self.window.show()

def main(cmd, inc=60): 
	# enter The four parameters are Interval event priority 
	# The simultaneous ordering of two events at the same time
	# A function that is invoked by calling
	# Arguments to this trigger function tuple form
	schedule.enter(0, 0, execute_command, (cmd, inc)) 

	# start GUI events
	gtk.main()
	return 0

if __name__ == "__main__":
	Buttons()
	# each 10 seconds Check network connections
	main("netstat -an", 10)
Is this a no-go approach, or is there still hope to get this working with some necessary mods to the script?
Reply
#2
Due to the overwhelming response and the impossible task to go through all the replies of the members of this great forum, I looked for other methods of solving this quest.

For anyone facing the same dillemma I have, I recommend looking into the following thread:
http://www.pygtk.org/pygtk2tutorial/sec-...ressbarfig

In particular def progress_timeout.
You are welcome.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020