Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Read .txt file in Glade GUI, python + glade

views
     
oshiri
post Mar 23 2009, 12:40 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
This is an example which I often use:

CODE
def mytxt(self):
 textview1 = self.wTree.get_widget("textview1")
 file = open('xxxx.txt')
 string = file.read()
 buffer = textview1.get_buffer()
 buffer.set_text(string)
 textview1.scroll_mark_onscreen(buffer.get_insert())
 file.close()
 return True

oshiri
post Mar 24 2009, 07:59 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
I remove your second window in glade file.
I also change button2 signal from button2_activated to button2_clicked.

Edited glade file:

CODE
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
[bad html removed]*- mode: xml -*-->
<glade-interface>
 <widget class="GtkWindow" id="window1">
   <property name="visible">True</property>
   <property name="title" translatable="yes">window1</property>
   <signal name="destroy" handler="on_window1_destroy"/>
   <child>
     <widget class="GtkVBox" id="vbox1">
       <property name="visible">True</property>
       <child>
         <widget class="GtkLabel" id="label1">
           <property name="visible">True</property>
           <property name="label" translatable="yes">Please press the Searching button to view the content:
</property>
         </widget>
         <packing>
           <property name="expand">False</property>
           <property name="fill">False</property>
         </packing>
       </child>
       <child>
         <widget class="GtkScrolledWindow" id="scrolledwindow1">
           <property name="visible">True</property>
           <property name="can_focus">True</property>
           <child>
             <widget class="GtkTextView" id="textview1">
               <property name="width_request">26</property>
               <property name="height_request">17</property>
               <property name="visible">True</property>
               <property name="can_focus">True</property>
               <property name="cursor_visible">False</property>
               <property name="overwrite">True</property>
               <signal name="show" handler="on_textview_show"/>
             </widget>
           </child>
         </widget>
         <packing>
           <property name="padding">10</property>
           <property name="position">1</property>
         </packing>
       </child>
       <child>
         <widget class="GtkHSeparator" id="hseparator1">
           <property name="visible">True</property>
         </widget>
         <packing>
           <property name="position">2</property>
         </packing>
       </child>
       <child>
         <widget class="GtkHBox" id="hbox1">
           <property name="visible">True</property>
           <child>
             <widget class="GtkLabel" id="label2">
               <property name="visible">True</property>
               <property name="label" translatable="yes">Please copy the content to text entry below:</property>
             </widget>
             <packing>
               <property name="expand">False</property>
               <property name="fill">False</property>
             </packing>
           </child>
         </widget>
         <packing>
           <property name="position">3</property>
         </packing>
       </child>
       <child>
         <widget class="GtkEntry" id="entry1">
           <property name="visible">True</property>
           <property name="can_focus">True</property>
           <property name="invisible_char">*</property>
           <signal name="insert_text" handler="on_entry1_insert_text"/>
         </widget>
         <packing>
           <property name="expand">False</property>
           <property name="fill">False</property>
           <property name="position">4</property>
         </packing>
       </child>
       <child>
         <widget class="GtkHButtonBox" id="hbuttonbox1">
           <property name="visible">True</property>
           <child>
             <widget class="GtkButton" id="button1">
               <property name="visible">True</property>
               <property name="can_focus">True</property>
               <property name="can_default">True</property>
               <property name="label">gtk-ok</property>
               <property name="use_stock">True</property>
               <property name="response_id">0</property>
             </widget>
           </child>
           <child>
             <widget class="GtkButton" id="button2">
               <property name="visible">True</property>
               <property name="can_focus">True</property>
               <property name="can_default">True</property>
               <property name="response_id">0</property>
               <signal name="clicked" handler="on_button2_clicked"/>
               <child>
                 <widget class="GtkAlignment" id="alignment1">
                   <property name="visible">True</property>
                   <property name="xscale">0</property>
                   <property name="yscale">0</property>
                   <child>
                     <widget class="GtkHBox" id="hbox2">
                       <property name="visible">True</property>
                       <property name="spacing">2</property>
                       <child>
                         <widget class="GtkImage" id="image1">
                           <property name="visible">True</property>
                           <property name="stock">gtk-find</property>
                         </widget>
                         <packing>
                           <property name="expand">False</property>
                           <property name="fill">False</property>
                         </packing>
                       </child>
                       <child>
                         <widget class="GtkLabel" id="label3">
                           <property name="visible">True</property>
                           <property name="label" translatable="yes">Searching</property>
                           <property name="use_underline">True</property>
                         </widget>
                         <packing>
                           <property name="expand">False</property>
                           <property name="fill">False</property>
                           <property name="position">1</property>
                         </packing>
                       </child>
                     </widget>
                   </child>
                 </widget>
               </child>
             </widget>
             <packing>
               <property name="position">1</property>
             </packing>
           </child>
         </widget>
         <packing>
           <property name="position">5</property>
         </packing>
       </child>
     </widget>
   </child>
 </widget>
</glade-interface>


For the python file, I edited and include button2 (Searching), so when you clicked button Searching it will show content of the text file.
And edited file for python file:

CODE
#!/usr/bin/env python
import os, os.path
import sys

try:
 import pygtk
  pygtk.require("2.0")
except:
  pass
try:
import gtk
  import gtk.glade
except:
sys.exit(1)

class result:

def __init__(self):

 self.gladefile = "project6.glade"
 self.wTree = gtk.glade.XML(self.gladefile)
 

 dic = { "on_button2_clicked" : self.on_button2_clicked,
 "on_window1_destroy" : gtk.main_quit}

 self.wTree.signal_autoconnect(dic)


def on_button2_clicked(self, widget):
 textview1 = self.wTree.get_widget("textview1")
 file = open('super.txt')
 string = file.read()
 buffer = textview1.get_buffer()
 buffer.set_text(string)
 textview1.scroll_mark_onscreen(buffer.get_insert())
 file.close()
 return True
 
 
 
 


if __name__ == "__main__":
gui = result()
gtk.main()



The output:
user posted image

oshiri
post Mar 25 2009, 10:08 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE(Law @ Mar 25 2009, 03:41 AM)
Oh yes, i did it...you are cool man...
you saved me...

It is not that hard as i thought


Added on March 25, 2009, 3:50 amHi Oshiri

I'm moving to my next step for my GUI now...

How can i execute a command on terminal through my GUI (when a button is clicked)...??

Should i connect the "button" signal to "run" in order to execute the commands line on my terminal?

Do you have any website link so that i can learn from there?
python + glade are very interesting
*
Better use signal clicked because you want to click button to do something.

There are several ways you can run thirdparty command fron python.

1. import os
os.system("command")
2. import os
stream = os.popen("command")
3. import subprocess
print os.popen("command").read()
4. import subprocess
print Popen("command", stdout=PIPE, shell=True).stdout.read()
5. import subprocess
return_code = call("command", shell=True)

http://www.python.org/doc/2.5/lib/module-subprocess.html
http://www.learningpython.com/2006/05/30/b...ygtk-and-glade/


oshiri
post Mar 27 2009, 07:17 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
I'm also having problem with sending output from command line to textview.
I don't know if it is possible.
The trick I always do is pass the output to a temporary file and read it to textview.

Example, same code as i gave you (this is for viewing "ps ax" output):

CODE
class result:

       def __init__(self):

               self.gladefile = "project6.glade"
               self.wTree = gtk.glade.XML(self.gladefile)


               dic = { "on_button2_clicked" : self.on_button2_clicked,
               "on_window1_destroy" : gtk.main_quit}

               self.wTree.signal_autoconnect(dic)

       def on_button2_clicked(self, widget):
               textview1 = self.wTree.get_widget("textview1")
               os.system('ps ax > output')
               file = open('output')
               string = file.read()
               buffer = textview1.get_buffer()
               buffer.set_text(string)
               textview1.scroll_mark_onscreen(buffer.get_insert())
               file.close()
               os.system('rm -f output')
               return True


This post has been edited by oshiri: Mar 27 2009, 07:20 PM
oshiri
post Mar 30 2009, 11:38 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Good news.... finaly I got it working without using temporary file.

The code:

CODE
       def on_button2_clicked(self, buffer):
               stdin, stdouterr = os.popen4('ps ax')
               for line in stdouterr.readlines():
                       textview1 = self.wTree.get_widget("textview1")
                       buffer = textview1.get_buffer()
                       buffer.insert(buffer.get_end_iter(), utf8conv(line))

oshiri
post Apr 1 2009, 09:00 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE(Law @ Mar 31 2009, 05:20 PM)
All right man !!!

You are surprising me...

Your codes is more simplified easier...

Do you have any  idea about this command <<< utf8conv(line) >>>>
*
That will convert character to utf8. Came from this:
encoding = locale.getlocale()[1]
utf8conv = lambda x : unicode(x, encoding).encode('utf8')

oshiri
post Apr 7 2009, 11:56 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Next time please don't add in previous post.
Post on a new thread so that your post will be on top and people will know it.

Okay... for anybody wants to learn pygtk and glade, I would like to suggest Tepache.
http://kefir.sourceforge.net/tepache/index.html

Debian user can just: apt-get install tepache

This application will create a basic python code from your glade file.


This is my example code using tepache for radiobutton.
This example will open different window when you choose different radionbutton and click OK button:

CODE
#!/usr/bin/env python
# -*- coding: UTF8 -*-

# Python module project2.py
# Autogenerated from project2.glade
# Generated on Tue Apr  7 11:22:14 2009

# Warning: Do not modify any context comment such as #--
# They are required to keep user's code

import os

import gtk

from SimpleGladeApp import SimpleGladeApp
from SimpleGladeApp import bindtextdomain

app_name = "project2"
app_version = "0.0.1"

glade_dir = ""
locale_dir = ""

bindtextdomain(app_name, locale_dir)


class Window1(SimpleGladeApp):

   def __init__(self, path="project2.glade",
                root="window1",
                domain=app_name, **kwargs):
       path = os.path.join(glade_dir, path)
       SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
   

   #-- Window1.new {
   def new(self):
       print "A new %s has been created" % self.__class__.__name__
   #-- Window1.new }

   #-- Window1 custom methods {
   #   Write your own methods here
   #-- Window1 custom methods }

   #-- Window1.on_window1_destroy {
   def on_window1_destroy(self, widget, *args):
       print "on_window1_destroy called with self.%s" % widget.get_name()
       gtk.main_quit()
   #-- Window1.on_window1_destroy }

   def on_window2_destroy(self, widget, *args):
       print "on_window1_destroy called with self.%s" % widget.get_name()
       gtk.main_quit()
   #-- Window2.on_window2_destroy }
   
   def on_window3_destroy(self, widget, *args):
       print "on_window1_destroy called with self.%s" % widget.get_name()
       gtk.main_quit()
   #-- Window3.on_window3_destroy }
   
   def on_window4_destroy(self, widget, *args):
       print "on_window1_destroy called with self.%s" % widget.get_name()
       gtk.main_quit()
   #-- Window4.on_window4_destroy }
   
   #-- Window1.on_button2_clicked {   def on_button2_clicked(self, widget, *args):
   def on_button2_clicked(self, widget, *args):
       print "on_button2_clicked called with self.%s" % widget.get_name()
       gtk.main_quit()
   #-- Window1.on_button2_clicked }

   #-- Window1.on_button1_clicked {
   def on_button1_clicked(self, widget, *args):
       print "on_button1_clicked called with self.%s" % widget.get_name()
       if self.radiobutton1.get_active():
           Window2()
       if self.radiobutton2.get_active():
           Window3()
       if self.radiobutton3.get_active():
           Window4()
    #-- Window1.on_button1_clicked }

   #-- Window1.on_radiobutton1_toggled {
   def on_radiobutton1_toggled(self, widget, *args):
       print "on_radiobutton1_toggled called with self.%s" % widget.get_name()
   #-- Window1.on_radiobutton1_toggled }

   #-- Window1.on_radiobutton2_toggled {
   def on_radiobutton2_toggled(self, widget, *args):
       print "on_radiobutton2_toggled called with self.%s" % widget.get_name()
   #-- Window1.on_radiobutton2_toggled }
   
   #-- Window1.on_radiobutton3_toggled {
   def on_radiobutton3_toggled(self, widget, *args):
       print "on_radiobutton3_toggled called with self.%s" % widget.get_name()
   #-- Window1.on_radiobutton3_toggled }


class Window2(SimpleGladeApp):

   def __init__(self, path="project2.glade",
                root="window2",
                domain=app_name, **kwargs):
       path = os.path.join(glade_dir, path)
       SimpleGladeApp.__init__(self, path, root, domain, **kwargs)

   #-- Window2.new {
   def new(self):
       print "A new %s has been created" % self.__class__.__name__
   #-- Window2.new }

   #-- Window2 custom methods {
   #   Write your own methods here
   #-- Window2 custom methods }


class Window3(SimpleGladeApp):

   def __init__(self, path="project2.glade",
                root="window3",
                domain=app_name, **kwargs):
       path = os.path.join(glade_dir, path)
       SimpleGladeApp.__init__(self, path, root, domain, **kwargs)

   #-- Window3.new {
   def new(self):
       print "A new %s has been created" % self.__class__.__name__
   #-- Window3.new }

   #-- Window3 custom methods {
   #   Write your own methods here
   #-- Window3 custom methods }


class Window4(SimpleGladeApp):

   def __init__(self, path="project2.glade",
                root="window4",
                domain=app_name, **kwargs):
       path = os.path.join(glade_dir, path)
       SimpleGladeApp.__init__(self, path, root, domain, **kwargs)

   #-- Window4.new {
   def new(self):
       print "A new %s has been created" % self.__class__.__name__
   #-- Window4.new }

   #-- Window4 custom methods {
   #   Write your own methods here
   #-- Window4 custom methods }


#-- main {

def main():
   window1 = Window1()
   #window2 = Window2()
   #window3 = Window3()
   #window4 = Window4()

   window1.run()

if __name__ == "__main__":
   main()

#-- main }



Screenshot:

user posted image

This post has been edited by oshiri: Apr 10 2009, 01:36 PM
oshiri
post Apr 10 2009, 01:30 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
You can compile it yourself.
It only requires python 2.3 and above development package and python-support

if you are using python2.5:
apt-get install python2.5-dev python-support
then compile.

This is another trick for debian user, sometimes work, sometimes don't..
Get tepache package from debian lenny:
wget http://http.us.debian.org/debian/pool/main...e_1.1-5_all.deb

Extract package:
dpkg --extract tepache_1.1-5_all.deb tepache_1.1-5_all
dpkg --control tepache_1.1-5_all.deb tepache_1.1-5_all/DEBIAN

Edit control file:
nano tepache_1.1-5_all/DEBIAN/control

Look for line that say:
Depends: python (>= 2.3), python-support (>= 0.7.1)

Change it to:
Depends: python (>= 2.3), python-support

Rebuld package:
dpkg --build tepache_1.1-5_all

Install package:
dpkg -i tepache_1.1-5_all.deb

This post has been edited by oshiri: Apr 10 2009, 03:09 PM
oshiri
post Apr 10 2009, 03:08 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
That error from dpkg --build?
It was my mistake...sorry. I have it repaired.
It should be:
dpkg --build tepache_1.1-5_all

Kefir came from the same author of tepache.
Probably new name for tepache.
I tried it..but it always give me syntax error.

Autconnect function and most of the function is called from SimpleGladeApp.py.
No need to touch this file.
You only need to modified py file with the same name of your glade file... see my example on radio button.


Added on April 10, 2009, 3:52 pmIf you want to compile tepache from source, get the source from debian (I can't find the original source from their site):
wget http://ftp.de.debian.org/debian/pool/main/...1.1.orig.tar.gz
wget http://ftp.de.debian.org/debian/pool/main/...e_1.1-5.diff.gz

Extract both file. I use unp ( get it using apt-get install unp):
unp tepache_1.1.orig.tar.gz
unp tepache_1.1-5.diff.gz

cd to tepache folder:
cd tepache-1.1

patch file:
patch -p1 < ../tepache_1.1-5.diff

Change permission:
chmod -R 777 debian

Build package:
dpkg-buildpackage -rfakeroot

Install:
cd ..
dpkg -i tepache_1.1-5_all.deb




This post has been edited by oshiri: Apr 10 2009, 03:52 PM
oshiri
post Apr 10 2009, 06:59 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Don't simply use my code. Use it as example only.
That code are based on my glade file.

To use tepache, enter the folder where your glade file resides.
Then run : tepache yourgladefile.glade
It wiil create SimpleGladeApp.py and yourgladefile.py yourgladefile.py.ori

example:
CODE
$ ls
tutorial.glade

$ tepache tutorial.glade
written file tutorial.py

$ ls
SimpleGladeApp.py  tutorial.glade  tutorial.py  tutorial.py.orig


Then test your py file (created by tepache).
Your gui will popup but nothing will works. Try clicking on button you created, you will see output on terminal.
That output will give you some clue.

For example:
When you click close button it will show:
on_button1_clicked called with self.button1

That will give you a clue which line you should edit.

Close your gui using ctrl + c.

Edit your py file, find def on_button1_clicked(self, widget, *args):
Then add:
gtk.main_quit()

Now the close button will work and really close your window.

p/s
I am in the process of creating tutorial on how to use pygtk, glade and tepache.
When it is done, it will be on my website.

This post has been edited by oshiri: Apr 10 2009, 07:01 PM
oshiri
post Apr 11 2009, 05:44 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
I just release glade2 with simplegladepython patched for debian.
This will integrate glade2 and tepache.
No need to run tepache manually from command line, just hit Build button on glade (after you set option and save your file).

user posted image

Get it from my site:
http://mambang.org.my/modules/news/
oshiri
post Apr 12 2009, 07:59 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
The package is in gzip. Extract it and you'll get all 3 files.
Since you already got tepache installed, you only need to install 2 first, then followed by 1.

QUOTE
Can i hide the other windows initially, window will pop up only when particular radiobutton is selected.?

See at the bottom of your python code, you'll find:
def main():

Comment (#) other windows you don't want to popup except the first window.
Look at my example, only window1 is shown.

CODE
def main():
  window1 = Window1()
  #window2 = Window2()
  #window3 = Window3()
  #window4 = Window4()


QUOTE
Am i correct if i want to run a command when a button is clicked, in the mean time save the output to txt file.

See at my previous example on using temporary file and read it latter and send it to textview.

QUOTE
I have a doubt, whether possible to run 2 or 3 command with ONLY ONE button is clicked???

Yes you can. Add your command to the same signal. You can use sleep in between command to wait for one command to finish then run another command.



This post has been edited by oshiri: Apr 12 2009, 08:02 AM
oshiri
post Apr 13 2009, 10:59 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Do you mind pasting your glade file or python file or both?
oshiri
post Apr 13 2009, 09:20 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE
On my second window, there is also a radiobutton (eg: radiobutton5)...but tepache only show the radiobutton signal until radiobutton4 (which is on first window)...

It seems like it doesn't link up all the signal button?
Or i did any mistakes?


Check again your glade file.
Start from radiobutton4 and above you did not define signal.
That is why tepache does not produce code for those buttons.


QUOTE
how should i define "self.window_show" in order to let the window2 and window3 show up when button is clicked?

Not sure what you want to do. Poping both wiindows at the same time?
If that the case, I don't thing it possible with radio button. Probably you need to use toggle button.

QUOTE
def window_show(self, widget, *args):
if self.on_radiobutton1.get_active():
  self.window2.show()
 
if self.on_radiobutton2.get_active():
  self.window3.show()

Try use widget name rather then signal handler.
ex: if self.radiobutton1.get_active()
oshiri
post Apr 15 2009, 12:43 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Manual code:

CODE
#!/usr/bin/env python

import sys

try:
       import pygtk
       pygtk.require("2.0")
except:
       pass
try:
       import gtk
       import gtk.glade
except:
       sys.exit(1)

class Window1:

       def __init__(self):

               self.gladefile = "project8.glade"
               self.root = "window1"
               self.wTree = gtk.glade.XML(self.gladefile, self.root)

               dic = { "on_window1_destroy" : gtk.main_quit,
                       "on_button1_clicked": self.on_button1_clicked,
                       "on_button2_clicked": gtk.main_quit
                       }

               self.wTree.signal_autoconnect(dic)

       def on_button1_clicked(self, widget, *args):
               if self.wTree.get_widget("radiobutton1").get_active():
                       Window2()

               if self.wTree.get_widget("radiobutton2").get_active():
                       Window3()

               if self.wTree.get_widget("radiobutton3").get_active():
                       Window4()
class Window2:

       def __init__(self):

               self.gladefile = "project8.glade"
               self.root = "window2"
               self.wTree = gtk.glade.XML(self.gladefile, self.root)

               dic = { "on_window1_destroy" : gtk.main_quit
                       }

               self.wTree.signal_autoconnect(dic)


class Window3:

       def __init__(self):

               self.gladefile = "project8.glade"
               self.root = "window3"
               self.wTree = gtk.glade.XML(self.gladefile, self.root)

               dic = { "on_window1_destroy" : gtk.main_quit
                       }

               self.wTree.signal_autoconnect(dic)

class Window4:

       def __init__(self):

               self.gladefile = "project8.glade"
               self.root = "window4"
               self.wTree = gtk.glade.XML(self.gladefile, self.root)

               dic = { "on_window1_destroy" : gtk.main_quit
                       }

               self.wTree.signal_autoconnect(dic)


if __name__ == "__main__":
       Window1()
       gtk.main()


No need to define all radiobuttons signal as I call widget directly.


CODE
I tried to use tepache to compile, i cant fetch the txt file to my textview...

Cannot help without looking at your code.


oshiri
post Apr 15 2009, 11:19 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE
A question to ask you here, if i just want the particular window show, i can ignore the class window 2 - 4 ??

Yes. Just create class you want to use.

QUOTE
And i curious that, there are a lot of ways writing the code with python, sometimes really make me pening....

Hehe... name of the language = python (ular sawa) ..always belit belit la.
If you are familiar with bash scripting concept, you'll find any scripting language not that hard.
oshiri
post Apr 15 2009, 03:57 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Actually this has been shown in my previous example on using temporary file.

CODE
     def on_button2_clicked(self, widget):
              textview1 = self.wTree.get_widget("textview1")
              os.system('ps ax > output')
              file = open('output')
              string = file.read()
              buffer = textview1.get_buffer()
              buffer.set_text(string)
              textview1.scroll_mark_onscreen(buffer.get_insert())
              file.close()
              os.system('rm -f output')
              return True


See the first command is to save ps ax output to output file.
Then the output is read to textview.
Followed by removal of output file.

Sleep is like pausing. Give some time for command to finish or what ever.
In python you can use:
time.sleep(n)

*n = int in seconds

Bash scripting is not a real programming language and by far similar to C.
But it is the mother of all scripting language and the simplest of all.
Compulsory if you want to be a system admin.
I won't employed anybody without this knowledge to be a system admin.
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

This post has been edited by oshiri: Apr 15 2009, 04:06 PM
oshiri
post Apr 16 2009, 11:19 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE
Yes, i have refer from the example you give me,
I want to read the output text only, but i don't want to show the output...

So I can ignore texview part right..?

yes


QUOTE
I want to confirm, is the way I did is the proper way in writing this functions?

You are right.
But most of the time we don't need sleep.

QUOTE
I try to put logo on my GUI, but when i run the python file the error msg shown as above...

is it bugs?

Yes it is a bug but not a libglade's bug.
It's glade2 bug.
What you can do is, edit your glade file manually.
Find the line where your image file is.
Put full path to where your image will be found.

ex:
<property name="icon">/usr/share/pixmaps/logedit.png</property>
oshiri
post Apr 16 2009, 09:44 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE
Amazing...it's worked...
But each time when i save my glade file, the image will not show again...

That is exepected. you need to edit everytime you save glade file.

CODE
I want to make my application run as super user, I saw a solution for this is to create another window then use << xterm -e >>>
Do you have any better idea for this??

Try this :

CODE
if os.geteuid() != 0:
print "You must be root."
exit(1)


** Be carefull when using this in Debian. By default Debian does not allowed gui to be run from su.
You need sux, gksu or kdesu to run gui from su.
oshiri
post Apr 17 2009, 10:06 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
QUOTE
I found that, run a GUI with root is a very bad idea...
Why?

Probably it has to do with security so you wil not screw your system or open a hole for others to gain access with full previledge to your system.

Back to python, you could actually run your gui from root using su-to-root (comes with Debian's menu package) with this code:

CODE
if os.geteuid() != 0:
print "You are not root. Going to su you."
os.system('su-to-root -X -c "python your.py" || exit 1')
exit(1)



Or if you want to use gksu:
CODE
if os.geteuid() != 0:
print "You are not root. Going to su you."
os.system('gksu -g -m "Masukkan Password Root Anda" "python your.py" || exit 1')
exit(1)


*I read somewhere that python has a module called gksu. But I can't find it anyewhere.

Or you can create a su-to root bash wrapper for your python.
example:
CODE
#! /bin/bash
# su-to-root python wrapper
# created by oshiri

MYPY="python your.py"
function check_su ()
{
GK=FALSE
if [ -f /usr/bin/su-to-root ]; then
       GK=TRUE
fi
if [ "$GK" = "FALSE" ]; then
       echo " You don't have su-to-root. Please apt-get install menu."
       exit 1
fi
}

function su_wrapper ()
{
       if [ "$UID" -ne 0 ]; then
               exec su-to-root -X -c "$MYPY" || exit 1
       fi
}


check_su
su_wrapper

exit 0


Or gksu bash wrapper for your python:
CODE
#! /bin/bash
# gksu python wrapper
# created by oshiri

MYPY="python your.py"
function check_su ()
{
GK=FALSE
if [ -f /usr/bin/gksu ]; then
       GK=TRUE
fi
if [ "$GK" = "FALSE" ]; then
       echo " You don't have gksu. Please apt-get install gksu."
       exit 1
fi
}

function su_wrapper ()
{
       if [ "$UID" -ne 0 ]; then
               exec gksu -g -m "Masukkan Password Root Anda" "$MYPY" || exit 1
       fi
}


check_su
su_wrapper

exit 0


This thread has become a tutorial place rather then Q&A.
What in here will be in my tutorial.... soon.




2 Pages  1 2 >Top
 

Change to:
| Lo-Fi Version
0.0274sec    0.62    7 queries    GZIP Disabled
Time is now: 18th December 2025 - 10:41 AM