I'm new to Python and even new it GUI with Python.
I'm using Glade to create my GUI.
My problem is that when I click a button in My GUI inter face the GUI freezes until the process is complete.
I'm been told that I need to use "threading" to create a new process.
I can't seem to find a good example or tutorial that will should how to do this.

So, I wrote out a small script and I was hoping some one could tweak it to work.

In the script there is a label and two buttons.

button1 makes label1 count to 1000 one second at a time.
while button1 is running the GUI is locked.

I would like to have button2 stop button1's process and I would also like to see label1 as it is counting.

here is the script.

#!/usr/bin/python                                                            
import gtk.glade                                                             
import gtk    
import time

class buttons:
	def __init__(self):
		xml = gtk.glade.XML ("project2.glade")
		w = xml.get_widget ('window1')
		w.connect ('destroy', self.quit)
		
		self.label1 = xml.get_widget('label1')
		
		self.button1 = xml.get_widget ('button1')
		xml.signal_connect ('on_button1_clicked', self.on_button1_clicked)

		self.button2 = xml.get_widget ('button2')
		xml.signal_connect ('on_button2_clicked', self.on_button2_clicked)

		gtk.main()

	def on_button1_clicked (self, button):
		number = 0
		while number < 1000:	
			number2 = str(number)
			self.label1.set_text (number2)
			number = number + 1
			time.sleep(1)
			

	def on_button2_clicked (self, button):
		print "Some how stop button1's actions"

	def quit (self, widget):                                                 
	        gtk.main_quit ()   


buttons()

and if it helps here is the glade file

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
	<widget class="GtkLabel" id="label1">
	  <property name="visible">True</property>
	  <property name="label" translatable="yes">label1</property>
	  <property name="use_underline">False</property>
	  <property name="use_markup">False</property>
	  <property name="justify">GTK_JUSTIFY_LEFT</property>
	  <property name="wrap">False</property>
	  <property name="selectable">False</property>
	  <property name="xalign">0.5</property>
	  <property name="yalign">0.5</property>
	  <property name="xpad">0</property>
	  <property name="ypad">0</property>
	  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
	  <property name="width_chars">-1</property>
	  <property name="single_line_mode">False</property>
	  <property name="angle">0</property>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>

      <child>
	<widget class="GtkButton" id="button1">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="label" translatable="yes">button1</property>
	  <property name="use_underline">True</property>
	  <property name="relief">GTK_RELIEF_NORMAL</property>
	  <property name="focus_on_click">True</property>
	  <signal name="clicked" handler="on_button1_clicked" last_modification_time="Mon, 21 Apr 2008 17:00:39 GMT"/>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkButton" id="button2">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="label" translatable="yes">button2</property>
	  <property name="use_underline">True</property>
	  <property name="relief">GTK_RELIEF_NORMAL</property>
	  <property name="focus_on_click">True</property>
	  <signal name="clicked" handler="on_button2_clicked" last_modification_time="Mon, 21 Apr 2008 17:00:43 GMT"/>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>

Thanks for the help.

I've been working on this and I'm closer...maybe. this is what I have so far:

#!/usr/bin/python                                                            
import gtk.glade                                                             
import gtk    
import time
import threading



class buttons:
	def __init__(self):
		xml = gtk.glade.XML ("project2.glade")
		w = xml.get_widget ('window1')
		w.connect ('destroy', self.quit)
		
		self.label1 = xml.get_widget('label1')
		
		self.button1 = xml.get_widget ('button1')
		xml.signal_connect ('on_button1_clicked', self.on_button1_clicked)

		self.button2 = xml.get_widget ('button2')
		xml.signal_connect ('on_button2_clicked', self.on_button2_clicked)

		gtk.main()

	def func1 (self):
		print "______________---------------------___________________"
		number = 0
		while number < 1000:	
			number2 = str(number)
			self.label1.set_text (number2)
			print number			
			number = number + 1
			time.sleep(1)

	def on_button1_clicked (self, button):	
	#gtk.threads_init()	
		self.t=threading.Thread(target=self.func1)
	#self.t.setDaemon(True)
		print 'A'
		self.t.start()

	def on_button2_clicked (self, button):
		print "Some how stop button1's actions"

	def quit (self, widget):                                                 
	        gtk.main_quit ()   


buttons()

Some Help would be great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.