Results 1 to 6 of 6

Thread: Python interacting with zenity

  1. #1
    Join Date
    Feb 2009
    Location
    Netherlands
    Beans
    784

    Python interacting with zenity

    After having googeled all afternoon I give up and start this thread. I have a commandline python script which I want to turn into a nautilus script. This requires that the script at some point uses a popup window to print a message instead of printing in the shell. The code was as follows:
    PHP Code:
       if ip_addr:
          print 
    "Now serving on http://%s:%s/" % (ip_addrhttpd.server_port
    I changed that successfully to
    PHP Code:
     if ip_addr:
          
    os.system('zenity --info --text="Now serving on http://%s:%s/"' % (ip_addrhttpd.server_port)) 
    So far so good. Now comes the tricky part. I want a window that enables me to cancel the script or approve. This would mean a zenity --queston window, with a 'cancel' and a 'ok' button. In zenity 'cancel' returns the value '1', and 'ok' returns the value '0'. But how do I tell the script to react on these buttons? 'Cancel' would mean 'sys.exit ()' and 'ok' would mean 'continue'. I checked loads of documentation on the subprocess possibility (which seems to have replaced the os.popen command) but I don't understand how it works or how to have it interact with a window. Who helps me out? It doesn't have to be with zenity by the way. A gtk-window or whatever comes standard with Ubuntu is also ok.

  2. #2
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python interacting with zenity

    PHP Code:
    #!/usr/bin/env python
    from subprocess import Popen

    proc
    =Popen("zenity --question --text='Quit now?'"shell=True )
    proc.communicate()
    if 
    proc.returncode:
        print 
    "Cancel was pressed"
    else:
        print 
    "Ok was pressed" 

  3. #3
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python interacting with zenity

    Hi.

    If you use
    Code:
    zenity --question
    ok returns 0
    cancel returns 256

    so you could do this

    PHP Code:
    #! /usr/bin/env python
    import os

    question 
    os.system("zenity --question --text="continue?")
    if question == 256:
        print "
    Operation canceled."
        exit()
    else:
        print "
    Operation continuing.
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  4. #4
    Join Date
    Feb 2009
    Location
    Netherlands
    Beans
    784

    Re: Python interacting with zenity

    Thanks for your replies. I found out that 'cancel' gives 256 indeed. I don't know why the wiki on zenity says otherwise. However, someone got it working with the following:
    PHP Code:
     if ip_addr:
          
    userschoice os.system('zenity --question --text "Does not matter now"')

       
    # To kill it if the users presses the cancel-button

       
    if '256' in str(userschoice):
          
    sys.exit (1
    Problem solved. Thanks both for your help.
    Last edited by VCoolio; March 5th, 2009 at 10:52 PM.

  5. #5
    Join Date
    Jun 2007
    Beans
    692

    Re: Python interacting with zenity

    Quote Originally Posted by VCoolio View Post
    Thanks for your replies. I found out that 'cancel' gives 256 indeed. I don't know why the wiki on zenity says otherwise.
    It does return one, but when you use os.system() you get the return value bit-shifted to the left by 8, because it uses the return code given by an internally used wait() call (see here for an explanation of its return code). To get the real return code you just shift it back.
    Code:
    >>> 256 >> 8
    1
    edit: If you want to get the correct rc right away, use the subprocess module
    PHP Code:
    >>> import subprocess
    >>> sp.Popen("zenity --question"shell=True)  # Dialog appeared after hitting enter, I clicked cancel
    >>> p.poll()
    1
    >>> 
    Last edited by imdano; March 5th, 2009 at 09:49 PM.

  6. #6
    Join Date
    Feb 2007
    Location
    New York
    Beans
    894
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python interacting with zenity

    First, install python-setuptools. Then, do this:

    Code:
    sudo easy_install pyzenity
    Then do this:

    PHP Code:
    if ip_addr:
      if 
    not PyZenity.Question("Now serving on http://%s:%s/" % (ip_addrhttpd.server_port)):
        
    sys.exit 
    "Please remember to do things the Ubuntu way. There is always more than one solution to a problem, choose the one you think will be the easiest for the user. ... Try to think as a green user and choose the simplest solution." — Code of Conduct

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •