LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2007, 07:56 AM   #1
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Rep: Reputation: 58
Python program help needed. And confused about self...


Hey guys I'll start my explaining a bit about my issues.

I have my main class being called which of course fires off the init function. The only way I've found to let functions in the class use objects created in the init is to put 'self' in front of them. From what I've read 'self' means pretty much means 'who called me'. So if you do __init__(self) self refers to the class. Any object like self.treeview would then be an object of the class not __init__? Then if you have a function like this under the class (not __init__)

def somefunction(self)

it gains access to self.treeview. Ok so my problem:

Class Fires
__init__ Fires

in __init__ self.window is created and bound to the main widget from the glade file. the self.window.connect is called hooking the window state up to a function in the class. When the window changes state, it goes to the function and the functin calls (self.window.hide()) and it works fine.

So, now I have an icon in the task area... (get what I'm trying to do?) The icon, icon menu, and connectios are all in the class. (not init) I can't seem to figure out how to get the activate for the icon(when u click it) access to the self.window object. I tried moving all of the statusicon stuff into the __init__ function but each time you click it it looks like it refires __init__ and opens another main window. Even if just the creation of the icon is in __init__ and the functions are in the class it still does this... I've been workig on this for 2 days so hopefully someone can help me.

Here is the relevent code:

Code:
class dmcalGTK:
    """The DMCal Application"""

    def __init__(self):

	    #Set the glade file
	    self.gladefile = "dmcal.glade"
	    self.wTree = gtk.glade.XML(self.gladefile, "dmcalMain")

	    self.window = self.wTree.get_widget("dmcalMain")
	    self.window.connect('window-state-event', self.window_event)

	    #Create our dictionary and connect it
	    dic = {"on_create_container_activate" : self.create_container_activate,
	           "on_dmcalMain_destroy" : self.dmcalMain_destroy,
	           "on_aboutMenuItem_activate" : self.showAbout,
                   "on_quitButton_clicked" : self.dmcalMain_destroy,
                   "on_newButton_clicked" : self.create_container_activate,
                   "on_addButton_clicked" : self.showFileSelection,
                   "on_quitmenuitem_activate" : self.dmcalMain_destroy,
                   "on_removeButton_clicked" : self.removecontainer,
                   "on_saveMenuItem_activate" : self.savelist}
	    self.wTree.signal_autoconnect(dic)

	    preferencesMI = self.wTree.get_widget("preferencesMenuItem")
	    preferencesMI.set_sensitive(0)

	    #Setup the treeview
	    self.treeview = self.wTree.get_widget('treeview2')
	    
	    #Create the liststore model to use with the treeview
	    #Pattern: ICON, NAME TEXT, PATH TEXT
	    self.treelist = gtk.ListStore(gtk.gdk.Pixbuf, str, str, str)
	    
	    #Attatch the liststore model to the treeview
	    self.treeview.set_model(self.treelist)
	    
	    #Get the icons
	    self.yes = self.treeview.render_icon('gtk-yes', gtk.ICON_SIZE_SMALL_TOOLBAR)
	    self.no = self.treeview.render_icon('gtk-no', gtk.ICON_SIZE_SMALL_TOOLBAR)
	    
	    #Setup the columns on the listview
	    
	    column = gtk.TreeViewColumn('', gtk.CellRendererPixbuf(), pixbuf=0)
	    self.treeview.append_column(column)
	    column = gtk.TreeViewColumn('Name', gtk.CellRendererText(), text=1)
	    self.treeview.append_column(column)
	    column = gtk.TreeViewColumn('Location', gtk.CellRendererText(), text=2)
	    self.treeview.append_column(column)
	    column = gtk.TreeViewColumn('Mount Point', gtk.CellRendererText(), text=3)
	    self.treeview.append_column(column)

	    self.treelist.append([self.yes, 'container.enc', '/home/nomb/container.enc', '/media/enc'])

	    self.treeselection = self.treeview.get_selection()
	    self.treeselection.select_path(0)
    
            if commands.getoutput("whoami") != "root":
                print "Non-Root - Limited functionality..."

	        #Disable create new container menu entry
	        newcontainerMenuItem = self.wTree.get_widget("create_container")
	        newcontainerMenuItem.set_sensitive(0)

                #Disable create new container button
	        newcontainerButton = self.wTree.get_widget("newButton")
	        newcontainerButton.set_sensitive(0)

                #Disable mount button
                mntButton = self.wTree.get_widget("mountButton")
                mntButton.set_sensitive(0)

                #Disable un-mount button
                unmntButton = self.wTree.get_widget("unmountButton")
                unmntButton.set_sensitive(0)
	    
	        #Set alert message to status bar
	        statusbar = self.wTree.get_widget("statusbar1")
	        statusbarid = statusbar.get_context_id("need root")
	        statusbar.push(statusbarid, "Some functions require root permissions and path... (su -)")


    def quit_cb(widget, data = None):
	if data:
	    data.set_visible(False)
	gtk.main_quit()

    def popup_menu_cb(widget, button, time, data = None):
	if button == 3:
	    if data:
		data.show_all()
		data.popup(None, None, None, 3, time)
	pass    

    def activate_icon_cb(widget, data = None):
        pass

    def icon_show_about(widget, data = None):
	dmcalDialog = dmcalAbout()
	result = dmcalDialog.run()

    statusIcon = gtk.StatusIcon()

    menu = gtk.Menu()
    menuItem = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
    menuItem.connect('activate', icon_show_about)
    menu.append(menuItem)
    menuItem = gtk.SeparatorMenuItem()
    menu.append(menuItem)
    menuItem = gtk.ImageMenuItem(gtk.STOCK_QUIT)
    menuItem.connect('activate', quit_cb, statusIcon)
    menu.append(menuItem)

    # statusIcon.set_from_stock(gtk.STOCK_HOME)
    statusIcon.set_from_file("pixmaps/dmcal.png")
    statusIcon.set_tooltip("DM-Crypt & LUKS Encryption Suite")
    statusIcon.connect('activate', activate_icon_cb)
    statusIcon.connect('popup-menu', popup_menu_cb, menu)
    statusIcon.set_visible(True)

    def window_event(self, widget, event):
	#print event.new_window_state
	if event.new_window_state == gtk.gdk.WINDOW_STATE_ICONIFIED:
	    self.window.hide()
Thanks everyone.

nomb
 
Old 11-15-2007, 09:20 AM   #2
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
Wow, 50 reads and not one comment. I guess no one uses python?
 
Old 11-15-2007, 10:48 AM   #3
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Rep: Reputation: 32
I tried to run your code and got the message inconsistent identation detected this could be part of the problem.
 
Old 11-15-2007, 11:14 AM   #4
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
Quote:
Originally Posted by davholla View Post
I tried to run your code and got the message inconsistent identation detected this could be part of the problem.
Nah, you can look at the code and see it's indented properly. The code runs fine. I'm just not sure how to code the next part. You probably got that message because I only gave you a section of it. Or when you pasted it in, it might have messed a line up.

You wont be able to run that code even when you fix the indention problem because you don't have the glade file.

Anyone know how to refer to the window once it is hidden from a function in the class?

nomb
 
Old 11-15-2007, 11:26 AM   #5
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Rep: Reputation: 32
Sorry I misread the question.
Am I right in thinking you want to open a new window when you click on a button.
 
Old 11-15-2007, 12:10 PM   #6
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
Quote:
Originally Posted by davholla View Post
Sorry I misread the question.
Am I right in thinking you want to open a new window when you click on a button.
Not exactly.

I run the program and the main window comes up with the status icon. When I minimize the window, instead of minimizing it, it hides it. So the window is gone but the icon is still in the tasktray. (minimizing to tasktray) What I am trying to accomplish is that when you click the tasktray icon (or whatever it is called in linux) then the window unhides and comes back.

everything works flawlessly except i can't get:

Code:
def activate_icon_cb(widget, data = None):
        pass
to have access to self.window from the __init__ to run the self.window.show(). I'have tried setting it up like:

Code:
def activate_icon_cb(self, widget, data = None):
        pass

# statusIcon.set_from_stock(gtk.STOCK_HOME)
statusIcon.set_from_file("pixmaps/dmcal.png")
statusIcon.set_tooltip("DM-Crypt & LUKS Encryption Suite")
statusIcon.connect('activate', activate_icon_cb(self))
statusIcon.connect('popup-menu', popup_menu_cb, menu)
statusIcon.set_visible(True)
but it says self isn't defined. When I tried recalling the widget from the glade file and showing it that way instead of showing it it just made another. But I have to bring the first on back up not make a new one.

Thanks for helping me with this.

nomb
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
needed python?? abd_bela Programming 2 06-28-2007 12:04 PM
Help Needed on configure FTP server on LINUX IPv6 :confused: rachel41 Linux - Server 0 04-30-2007 07:37 AM
A confused program Gins Programming 10 03-28-2006 02:45 AM
PYTHON svn module needed with Trac humbletech99 Programming 0 11-30-2005 12:01 PM
Python Prob....Solution needed urgently!!! abhigo Programming 5 02-10-2005 05:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration