Advertisement
metalx1000

Python GTK with very simple Threading

Dec 19th, 2014
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import pygtk
  4. pygtk.require('2.0')
  5. import gtk,os,thread
  6.  
  7.  
  8. class BasicButtons():
  9.  
  10.     def msg(self, widget, msg):
  11.         print(msg + " was clicked")
  12.         if msg == "Exit":
  13.             self.destroy(widget)
  14.         elif msg == "Get Files":
  15.             thread.start_new_thread(self.list_files,("/media","Files:"))
  16.         elif msg == "Print Files":
  17.             for f in self.files:
  18.                 print(f)
  19.  
  20.     def list_files(self, dir, msg):
  21.         print(msg)
  22.         self.files = []
  23.         for root, dirnames, filenames in os.walk(dir):
  24.             self.files += filenames
  25.  
  26.     def destroy(self, widget, data=None):
  27.         print("Destroyed!")
  28.         print("What a world!\nWhat a world!")
  29.         gtk.main_quit()
  30.  
  31.     def __init__(self):
  32.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  33.         self.window.connect("destroy", self.destroy)
  34.         self.window.set_border_width(10)
  35.         self.window.set_size_request(720,480)
  36.         self.window.set_position(gtk.WIN_POS_CENTER)
  37.         self.window.set_title("My GTK Button Program")
  38.         self.box = gtk.VBox()
  39.         self.window.add(self.box)
  40.         self.box.show()
  41.  
  42.         self.buttons = ['On','Off','Next','Prev', 'Get Files', 'Print Files', 'Exit']
  43.         for b in self.buttons:
  44.             self.button = gtk.Button(b)
  45.             self.button.connect("clicked", self.msg, b)
  46.             self.box.pack_start(self.button)
  47.             self.button.show()
  48.  
  49.         self.window.show()
  50.  
  51.     def main(self):
  52.         gtk.main()
  53.  
  54. if __name__ == "__main__":
  55.     Buttons = BasicButtons()
  56.     Buttons.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement