Python program help needed. And confused about self...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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()
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?
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.