Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Read .txt file in Glade GUI, python + glade

views
     
TSLaw
post Mar 23 2009, 03:01 AM, updated 17y ago

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
I'm currently doing a simple application by using python + glade...

Can anyone roughly tell me how to read my .txt file on textview? i tried google-ing, i should use buffer() function to read right?

I was doing this for few days, but still cant get the .txt file appear on my GUI application...

Can someone help me up ??

Thanks...
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

TSLaw
post Mar 24 2009, 05:24 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
bro oshiri,

I stil cant get the text file on my GUI screen...

Do you mind to have a look on my code...

Thanks & appreciate your help...


My idea is when the "searching button" is clicked...then the text will automatically show up on the textview, but it seems like i failed to do it...

This post has been edited by Law: Mar 24 2009, 05:28 PM


Attached File(s)
Attached File  folder.tar.gz ( 2.37k ) Number of downloads: 38
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

TSLaw
post Mar 25 2009, 03:41 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
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

This post has been edited by Law: Mar 25 2009, 03:50 AM
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/


TSLaw
post Mar 25 2009, 12:22 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Thanks for your reply

I need some time to read through them, i will try by myself first...

If anything i cant solve or understand, hope you can advise me here...

Thanks again...


Added on March 26, 2009, 5:41 pmHi oshiri,

For example:

i would like to run this command on terminal through my GUI:

CODE


hcitool scan hci0



This is bluetooth scanning command.

I would like to scan the bluetooth devices when a <<button>> is clicked on my GUI...
After the scanning process is finished, the available bluetooth devices will be shown on my textview...

Can i do this with python...??

Thanks again and looking forward your advises...


Added on March 26, 2009, 8:19 pmi get this pygtk example...

Im trying to done this with glade instead of pygtk...

here is the code:

CODE
#!/usr/bin/env python
"""Show a shell command's output in a gtk.TextView without freezing the UI"""

import os, threading, locale

import gobject
import gtk

gobject.threads_init()
gtk.gdk.threads_init()

encoding = locale.getpreferredencoding()
utf8conv = lambda x : unicode(x, encoding).encode('utf8')

def on_button_clicked(button, view, buffer, command):
    thr = threading.Thread(target= read_output, args=(view, buffer, command))
    thr.start()

def read_output(view, buffer, command):
    stdin, stdouterr = os.popen4(command)
    while 1:
        line = stdouterr.readline()
        if not line:
            break
        gtk.gdk.threads_enter()
        iter = buffer.get_end_iter()
        buffer.place_cursor(iter)
        buffer.insert(iter, utf8conv(line))
        view.scroll_to_mark(buffer.get_insert(), 0.1)
        gtk.gdk.threads_leave()

sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
textview = gtk.TextView()
textbuffer = textview.get_buffer()
sw.add(textview)
win = gtk.Window()
win.resize(300,500)
win.connect('delete-event', gtk.main_quit)
button = gtk.Button(u"Press me!")
command = 'ls -R %s' % (os.getcwd(),)
button.connect("clicked", on_button_clicked, textview, textbuffer, command)
vbox = gtk.VBox()
vbox.pack_start(button, False)
vbox.pack_start(sw)
win.add(vbox)
win.show_all()

gtk.main()


I got this error: AttributeError: result instance has no attribute 'textbuffer' with a modified code from above...

This post has been edited by Law: Mar 26 2009, 08:19 PM
TSLaw
post Mar 27 2009, 12:02 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
CODE
#!/usr/bin/env python
import os, os.path, threading, locale
import sys
try:
 import pygtk
  pygtk.require("2.0")
except:
  pass
try:
import gtk
  import gtk.glade
import gobject
except:
sys.exit(1)

encoding = locale.getpreferredencoding()
utf8conv = lambda x : unicode(x, encoding).encode('utf8')


class result:

def __init__(self):

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

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

 self.wTree.signal_autoconnect(dic)

 
def on_scan(self, obj):
 thr = threading.Thread(target= self.read_output, args=(self.textbuffer, self.command))
 thr.run()

 self.command = "hcitool scan"
 textview1 = self.wTree.get_widget("textview1")
 self.textbuffer = textview1.get_buffer()
 textview1.scroll_mark_onscreen(buffer.get_insert())  
 

def read_output(self, buffer, command):
 stdin, stdouterr = os.popen4(command)
 for line in stdouterr.readlines():
   buffer.insert(buffer.get_end_iter(), utf8conv(line))




 


if __name__ == "__main__":
rst = result()

gtk.main()
 
 





Is there any wrong on this code??
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
TSLaw
post Mar 27 2009, 10:50 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Yeah...

This question is driving me crazy...

Let's do it together and share our knowledge together here...

I really like python...
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))

TSLaw
post Mar 31 2009, 05:20 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
All right man !!!

You are surprising me...

Your codes is more simplified easier...

Do you have any idea about this command <<< 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')

TSLaw
post Apr 1 2009, 04:27 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
The next thing I would like to learn will be radio button...

Seems like not much tutorial about radio button can be found online...


Added on April 1, 2009, 6:47 pm
CODE


self.window1 = gtk.glade.XML ("project5.glade")
 self.window2 = self.window1.get_widget("window2")

 dic = { "on_window1_destroy" : gtk.main_quit,
  "on_radiobutton1_toggled" : self.on_rd1,
  "on_radiobutton2_toggled" : self.on_rd2,
  "on_radiobutton3_toggled" : self.on_rd3,
  "on_button1_clicked" : self.window2_show
 
  }

 self.window1.signal_autoconnect(dic)
 window1 = self.window1.get_widget("window1")

def on_rd1(self, widget):
                      if self.on_rd1.get_active():
                      self.on_rd1 = "window2"
               else:
                      self.on_rd2 = "window3"
                      self.on_rd3 = "window4"
 

def window2_show(self, widget):
              self.window2_show
              return True



I want to make one of the radio button get activated and proceed to next window when <<OK>> is clicked, the code will be something as above right?

I think i missed up something...



This post has been edited by Law: Apr 1 2009, 06:47 PM
TSLaw
post Apr 6 2009, 06:14 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Anyone??

Help me out please...
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
TSLaw
post Apr 10 2009, 11:47 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Tepache only support for lenny version for debian..

btw im using etch version...
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
TSLaw
post Apr 10 2009, 01:57 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Ok...thanks much..

I got found one application called kefir which is having similar functions as tepache.

I don't see any autoconnect function from the code sketched by tepache, but it was done with simple code.

This post has been edited by Law: Apr 10 2009, 02:09 PM
TSLaw
post Apr 10 2009, 02:35 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
It shows me error:

dpkg-deb: failed to open package info file `tepache_1.1-5_all.deb/DEBIAN/control' for reading: Not a directory

i have checked on the directory, the file is exist.

so is it meant that this method doesn't work for me?

thanks...

This post has been edited by Law: Apr 10 2009, 02:39 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
TSLaw
post Apr 10 2009, 04:55 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
I think i have success installed tepache, nothing was appeared.
How am i going to start the tepache?

I also modified the name in glade with the same as your code, but it show me error:

ImportError: No module named SimpleGladeApp

Attached you a printscreen photo of installation result:




Attached thumbnail(s)
Attached Image
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
TSLaw
post Apr 10 2009, 07:12 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
okok...terima kasih you...

looking forward your tutorial, once completed remember drop me a msg...

i will try it later...
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/
TSLaw
post Apr 11 2009, 11:09 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
I have tried to build the radiobutton tutorial by tepache, but all the windows is pop up when i run the file.

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


Added on April 11, 2009, 11:18 pmIt seems like quite complicated to construct huh...?

So all i need now is download the file:

1. glade_2.12.1-7_i386.deb
2. glade-common_2.12.1-7_i386.deb
3. tepache_1.1-5_all.deb

For 1 and 3, i think i got in my linux, but i have no idea about 2...

How to start with this 3 files..?

This post has been edited by Law: Apr 11 2009, 11:20 PM
TSLaw
post Apr 12 2009, 12:27 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
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.

CODE
def show_searching(self, obj):
 os.system('dmesg > output.txt')
 self.searching.show()
 return True

(example for command dmesg )

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

Thanks for advise!!!

This post has been edited by Law: Apr 12 2009, 12:32 AM
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
TSLaw
post Apr 13 2009, 10:14 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
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?
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?
TSLaw
post Apr 13 2009, 11:21 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
ok...sure...but i need to go out now...
i will paste on here by tonight, you can take your time to see...

thanks...
TSLaw
post Apr 13 2009, 05:38 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Hi Oshiri,

This is my glade file together with python file.

Hope you can take a look on it...

Thanks...



This post has been edited by Law: Apr 13 2009, 06:23 PM


Attached File(s)
Attached File  radiobutton.tar.gz ( 8.67k ) Number of downloads: 6
TSLaw
post Apr 13 2009, 06:25 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Btw, i try to write the code by myself with python...

In the glade file, i connect radiobutton signal as below

CODE

"on_radiobutton1_toggled" : self.on_radiobutton1,
"on_radiobutton2_toggled" : self.on_radiobutton2,
"on_button1_clicked" : self.window_show


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

I do it as this way...


CODE


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


It show me error that couldn't create the XML file, so i think i need to define the "on_radiobutton1" & on_radiobutton2"

What should i put for this?

This is my first try, i will continue trying...
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()
TSLaw
post Apr 13 2009, 11:11 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
QUOTE(oshiri @ Apr 13 2009, 09:20 PM)
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.
Nope, i want only a window is pop up each time when button is clicked...just like the glade+python file i attached...but write the code manually...
Can you do me a favor, show me how to do this?
The same thing of radiobutton example, but can show how to construct the code manually??

QUOTE
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.
I keep checking on first window, but never notice the mistake here...
Thanks...

This post has been edited by Law: Apr 14 2009, 02:23 AM
TSLaw
post Apr 14 2009, 01:07 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Hi oshiri,

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

I saw there is a place for us to write in our own code...

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.


TSLaw
post Apr 15 2009, 01:07 AM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Oh man...
I tried the radiobutton for almost one week...but got nothing...

You are my god man...

I really really wish to see your glade + python + tepache tutorial soon...

Hope to hear you soon....


Added on April 15, 2009, 1:17 amA question to ask you here, if i just want the particular window show, i can ignore the class window 2 - 4 ??

By the way, the way you did is more tidy and easy to let others understand...

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

I will keep learning...




This post has been edited by Law: Apr 15 2009, 01:17 AM
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.
TSLaw
post Apr 15 2009, 02:07 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
QUOTE(oshiri @ Apr 15 2009, 11:19 AM)
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.
*
HAHA...funny and good answer...

I want stick one way in writing python first...after i getting familiar with this, i will learn other...

Bash script is something like C programming ??
TSLaw
post Apr 15 2009, 02:11 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Regarding to previous post...I did asked you about running 2 commands with one button is clicked...

About the time sleep, how to do that??

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
TSLaw
post Apr 15 2009, 04:12 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Hi Oshiri

QUOTE
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.


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..?

QUOTE
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
For this one, i have insert time.sleep(0.5) between each of the commands...

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

CODE

example:

command 1
time.sleep(n)
command 2


By the way, It can run with no problem...

Again thanks much

This post has been edited by Law: Apr 16 2009, 04:28 AM
TSLaw
post Apr 15 2009, 11:32 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
CODE

(version2.py:12312): libglade-WARNING **: Error loading image: Failed to open file './logoedit.png': No such file or directory

(version2.py:12312): libglade-WARNING **: could not convert string to type `GdkPixbuf' for property `icon'

(version2.py:12312): libglade-WARNING **: Error loading image: Failed to open file './bt_logo.png': No such file or directory

(version2.py:12312): libglade-WARNING **: could not convert string to type `GdkPixbuf' for property `logo'


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

is it bugs?
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>
TSLaw
post Apr 16 2009, 01:57 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
QUOTE(oshiri @ Apr 16 2009, 11:19 AM)
yes
You are right.
But most of the time we don't need sleep.
So you mean that i can just put like this:

command1
command2
command3

because the time.sleep() made slow response to my application...
ignore this should be alright, my command to be enter here is only to create a new directory...

QUOTE
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>
*
Amazing...it's worked...
But each time when i save my glade file, the image will not show again...



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??

I almost end to my "small application" ...very happy and glad have your help....
Hope to hear you soon...

I'm waiting for you tutorial... brows.gif

This post has been edited by Law: Apr 16 2009, 02:15 PM
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.
TSLaw
post Apr 16 2009, 11:35 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
I have googling around...

I found that, run a GUI with root is a very bad idea...
Why?


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.



TSLaw
post Apr 18 2009, 07:35 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Hehe^^...

Really become a tutorial already...

Still editing my python code now...
TSLaw
post Apr 21 2009, 05:28 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Hi Oshiri,

When your tutorial will be done?

What package is include in your OshiriX...

Maybe will try it...
TSLaw
post Apr 21 2009, 05:56 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
Oh ya...

Is there any way to let a window shows immediately after a command button is clicked ??
because the window is waiting for the command running...and it will only show up after command complete running...


oshiri
post Apr 21 2009, 09:07 PM

Enthusiast
*****
Senior Member
751 posts

Joined: Nov 2004
Tutorial ...it's done. Check my site.
6 part all together. Will add more later, taking a break right now.
Right now, my interest is in php-gtk.

Oshirix, it's a hobby. Latest is Oshirix2009 , DVD. Multi desktop.
There is no way you can download it now. The download "window" only open in short period of time.
There is another version, cd, my experiment with netbook interfaces. Mainly for troubleshooting purposes.
Not for public.

QUOTE
Is there any way to let a window shows immediately after a command button is clicked ??
because the window is waiting for the command running...and it will only show up after command complete running...

Try send you command to background using &

This post has been edited by oshiri: Apr 21 2009, 09:09 PM
TSLaw
post Apr 23 2009, 12:46 PM

Getting Started
**
Junior Member
134 posts

Joined: Apr 2005
I have read through all your tutorials already...
great job dude...

very good for anyone who want to learn python + glade + tepache...

Very powerful !!!

 

Change to:
| Lo-Fi Version
0.4366sec    0.45    6 queries    GZIP Disabled
Time is now: 15th December 2025 - 01:47 AM