GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 24, 2014 11:28 am

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: PyGTK button events
PostPosted: Sat Dec 10, 2011 2:32 am 

Joined: Sat Dec 10, 2011 2:26 am
Posts: 2
I am trying to get a button click to set some text in an Entry. Below is my code.


import sys
import os

importStatus = False
filenametf = None

   import pygtk
   import gtk
   importStatus = True

except ImportError:
    print "PyGTK module does not exist. Can't launch GUI !"
    print "Please download and install GTK and PyGTK."
    importStatus = False

if importStatus:

    class gtkGUI():

        output = None

        def __init__(self):
            print "Starting GTK Interface"

        def startGUI(self):
            print "GUI Started"
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            self.window.set_title("Shredder - v0.1 (Alpha)")
            self.window.connect("destroy", self.destroy)

            ## Labels
            self.filelbl = gtk.Label("File / Folder: ")
            self.iterationlbl = gtk.Label("Shred Iterations: ")
            self.zerolbl = gtk.Label("Zero-ing:")

            ## Buttons
            self.shredbtn = gtk.Button("Shred !")
            self.filechoosebtn = gtk.Button("...")
            self.trashbtn = gtk.Button("Trash")
            self.trashbtn.connect("clicked", self.get_trash_callback, None)

            ## Check Boxes
   = gtk.CheckButton(label=None)

            ## Text Fields
            self.filenametf = gtk.Entry(max=0)
            self.itertf = gtk.Entry(max=0)

            ## Text View with frame wrapping
            self.page_size = gtk.Adjustment(lower=100, page_size=100)
            self.sw = gtk.ScrolledWindow(self.page_size)
            self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            self.output = gtk.TextView()
            self.frame = gtk.Frame()
            self.frame.set_label(" Status: ")

            ## Tooltip Text

            # Shred button
            self.shredtooltip = gtk.Tooltips()
            self.shredtooltip.set_tip(self.shredbtn, "Begin Shredding Files")

            # File choose button
            self.filestooltip = gtk.Tooltips()
            self.filestooltip.set_tip(self.filechoosebtn, "Choose files or folder")

            # Trash button
            self.trashtooltip = gtk.Tooltips()
            self.trashtooltip.set_tip(self.trashbtn, "Shred Trash bin")

            ## Packing widgets into window

            # Vertical box to contain all boxes
            self.vbox = gtk.VBox(homogeneous=False, spacing=0)

            # filebox to contain file-based widgets
            self.filebox = gtk.HBox(homogeneous=False, spacing=0)
            self.filebox.pack_start(self.filelbl, expand=False, fill=False, padding=10)
            self.filebox.pack_start(self.filenametf, expand=False, fill=False, padding=0)
            self.filebox.pack_start(self.filechoosebtn, expand=False, fill=False, padding=10)
            self.filebox.pack_start(self.trashbtn, expand=False, fill=False, padding=0)
            self.vbox.pack_start(self.filebox, expand=False, fill=False, padding=0)

            # sctrlbox to contain shred control widgets
            self.sctrlbox = gtk.HBox(homogeneous=False, spacing=0)
            self.sctrlbox.pack_start(self.iterationlbl, expand=False, fill=False, padding=10)
            self.sctrlbox.pack_start(self.itertf, expand=False, fill=False, padding=0)
            self.sctrlbox.pack_start(self.zerolbl, expand=False, fill=False, padding=10)
            self.sctrlbox.pack_start(, expand=False, fill=False, padding=0)
            self.vbox.pack_start(self.sctrlbox, expand=False, fill=False, padding=5)

            # Add seperator
            self.separator = gtk.HSeparator()
            self.vbox.pack_start(self.separator, expand=False, fill=False, padding=5)           

            # Output View
            self.vbox.pack_start(self.frame, expand=True, fill=True, padding=5)

            # Shred button
            self.shredbox = gtk.HBox(homogeneous=False, spacing=0)
            self.shredbox.pack_end(self.shredbtn, expand=False, fill=False, padding=5)
            self.vbox.pack_end(self.shredbox, expand=False, fill=False, padding=5)

            ## Presenting window
            return None

        def checkGtkVersion(self):
            return gtk.gtk_version
        def checkPyGTKVersion(self):
            return gtk.pygtk_version

        def destroy(self, widget, data=None):
            return gtk.main_quit()

        def insertText(self, text):
            if(self.output == None):
                print "Empty Output"
                self.buffer = self.output.get_buffer()

        def get_trash_callback(widget, event, data):
            loc = os.getenv("HOME")
            print loc + "/.local/share/Trash"
            filenametf.set_text(loc + "/.local/share/Trash")

I am trying to fix the get_trash_callback() event from the code above as that's the cause of my problem for now. When I run the above code and click on the "Trash" button, I get the following output on my console:

Traceback (most recent call last):
  File "....../", line 145, in get_trash_callback
    filenametf.set_text(loc + "/.local/share/Trash")
AttributeError: 'NoneType' object has no attribute 'set_text'

If the above codes I posted are too huge and hard to read, do inform me quickly so I could reduce it down as much as possible to the essentials hopefully without altering the problems.

I attempted to use "self.filenamtf" in the line 145 code but it gives me "NameError: global name 'self' is not defined" if I added a "self" in front of "filenametf" on line 145.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group