6

Is it possible in a bash shell script (single file) with loops variables etc that executes zenity among other programs to remove zenity and add GTK functions instead?

If yes then is it possible also to add bash commands, variables, loops (copied from the bash shell script) to qt4 designer?

What i'm asking is if there a possibility that GTK functions can be combined with bash commands inside a bash shell script or even bash commands with qt4 designer.

I want to get rid of zenity (or yad, kdialog) because is very limited to a more advanced windows and still use all the rest bash functions that there already there in the bash shell script, is it possible?

  • yad is many times more powerful than zenity and probably the best solution for your objectives. – WinEunuuchs2Unix Nov 27 '17 at 5:26
  • sadly zenity, yad , kde dialogs they have limitations please take a look what you can do with qt designer, qt creator or glade – GoldHaloWings Nov 27 '17 at 5:32
  • You could always embed a python script within the bash file, and execute that script.... – mdd Nov 27 '17 at 5:42
  • 4
    There's a "loadable module" for bash called ctypes.sh which provides ways to call library functions, at the cost of selling your soul to the devil – grawity Nov 27 '17 at 8:11
  • 2
    @grawity That's the actual answer: that this can be done, but shouldn't. Are you interested in posting an answer showing specifically how to write a simple GTK+ application in Bash using ctypes.sh? That would be great... but if you don't want to, no one will blame you! Still, I think an answer explaining why and how ctypes.sh makes this possible, as well as why one really shouldn't do it in any script that performs an important task or is given to others to run (except as a demonstration of the power of bash plugins in general and ctypes.sh in particular), would also be quite welcome. – Eliah Kagan Nov 27 '17 at 11:32
11

There are no "GTK commands" in the way there are GTK+ functions in Python. GTK+ is a library with bindings in several languages, but it doesn't have executable commands for the functions it provides. You can try to do parts of what the GTK+ API can do via some external commands:

  • zenity, yad, etc. for showing dialog boxes
  • xsel or xclip to access the clipboard
  • wmctrl for controlling application windows

But the vast majority of GTK+ functionality can't be accessed by commands.

  • Thank you for telling me there actually not commands but functions but is it possible to add bash commands, variables, loops (copied from a bash shell script) as signals to qt4 designer? – GoldHaloWings Nov 27 '17 at 4:06
  • @GoldHaloWings If you mean signal handlers, AFAIK QML signal handlers are in JavaScript (with C++ a possibility). See doc.qt.io/qt-4.8/qmlevents.html. No bash. – muru Nov 27 '17 at 4:13
  • youtube.com/watch?v=R11v_iGWgn4&t=3m30s i know is not the qt designer but glade you get the idea what i mean – GoldHaloWings Nov 27 '17 at 4:25
7

Shells are just command interpreters as per POSIX definition. Gtk is a library, and meant to be imported in actual programming languages. So the answer is no, you can't use full-blown Gtk stuff in shell scripts, only the limited set of things that yad and zenity allow.

But you can use Python. It's a scripting language, yet more suitable for system and programming stuff than shells. You can call commands stored in places like /bin or /usr/bin via subprocess module in Python. I've done so many times for my Gtk apps.

Here's for example a standard function I use for calling external commands from Python script:

def run_cmd(self, cmdlist):
    """ Reusable function for running external commands """
    new_env = dict(os.environ)
    new_env['LC_ALL'] = 'C'
    try:
        stdout = subprocess.check_output(cmdlist, env=new_env)
    except subprocess.CalledProcessError:
        pass
    else:
        if stdout:
            return stdout

And here's an example using it in my xrandr-indicator for switching screen resolution from Ubuntu's top panel; as the name suggests, it calls xrandr behind the scenes :

    self.run_cmd(['xrandr','--output',out,'--mode',mode]) 

As for shell, you'd need to call a shell with -c argument. So something like this could work:

subprocess.Popen(['bash','-c', 'echo hello world'])

Alternatively, consider implementing interprocess communication. Make GUI in python, but let it communicate with a shell script via named pipe or file.

  • That's sad (for me) because i spend hours of making a bash shell script : ( So i stick to zenity, yad kdialog for now right? – GoldHaloWings Nov 27 '17 at 5:55
  • @GoldHaloWings yeah, just stick with those, and start slowly picking up on Python. I used to write everything in bash only but after awhile I started with Python, and it just became much easier to do GUI stuff plus file processing, at least for me personally – Sergiy Kolodyazhnyy Nov 27 '17 at 5:58
  • @GoldHaloWings you're welcome. Also, I've updated my answer – Sergiy Kolodyazhnyy Nov 27 '17 at 6:16
  • A funny question, if there is a way to exchange everything you know about Python in return to instantly learn C++ will you do it? – GoldHaloWings Nov 27 '17 at 8:44
  • 2
    Maybe worth noting that for GTK to function, gtk_init() must be first called, then you allocate some resources like GtkWidgets. And all this context is simply lost when the app which created it exits. This precludes any possibility for GTK "commands" unless one is willing to have a "GTK daemon" or some other entity executing the commands passed to it and storing the context. – Ruslan Nov 27 '17 at 9:29
0

I agree with @Sergiy that using only Python would be ideal. But i think of a better compromise. Rather than running Bash code from Python as proposed. It should be possible to keep the Bash code and to only run GTK from Python. Something like this :

exec 3> >(python3 &>/dev/null)

cat >&3 <<__CODE__
# python init gtk ...
__CODE__

# bash code ...
echo "..." >&3   # run a gtk command
# bash code ...

exec 3>&-
  • In theory, I like this idea... but is it practical for Bash to receive information from the python3 interpreter? In particular, doesn't the Bash script need to wait on the asynchronous python3 process every time there is a Bash command that requires the result of user interaction? Unless that problem is solved, everything that uses information gleaned from the user via GTK+ will have to be done in Python as well. – Eliah Kagan Nov 27 '17 at 12:33
  • Right, I can t find a correct way of doing this. So this solution is actually limited. – user285259 Nov 28 '17 at 18:35
  • From a user point of view Python is using alot of cpu, i also used Py script in my bash script. I was thinking to start learning JavaScript which is far more faster and easy. But i said hey i have time and patience C++ is my personal choice which i learning and i practice btw. I know (few things from bash scripting) what is a variable and if and else statements hexadecimal etc and i feel can learn more about C++ I don't say that is an easy task thow, but i want to test my limits by challenging my self to learn C++ I believe C++ with QT Creator is exactly what i need (i believe) – GoldHaloWings Nov 29 '17 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.