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?

GoldHaloWings
  • 517
  • 3
  • 10
  • `yad` is many times more powerful than `zenity` and probably the best solution for your objectives. – WinEunuuchs2Unix Nov 27 '17 at 05: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 05:32
  • You could always embed a python script within the bash file, and execute that script.... – mdd Nov 27 '17 at 05:42
  • I know that but already have created a bash shell script and i know only bash so is it possible to write gtk functions inside the bash shell script? or it must be done only with python? – GoldHaloWings Nov 27 '17 at 05:50
  • 5
    There's a "loadable module" for bash called [ctypes.sh](http://ctypes.sh/) which provides ways to call library functions, at the cost of selling your soul to the devil – user1686 Nov 27 '17 at 08:11
  • Quite complex but interesting – GoldHaloWings Nov 27 '17 at 08:34
  • 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
  • The ctypes.sh repository already has a GTK example. – user1686 Nov 27 '17 at 11:40
  • @grawity Sounds like an answer can be posted, then... – Eliah Kagan Nov 27 '17 at 11:48
  • @GoldHaloWings as someone who's had to write and maintain lots of shell scripts, small and big: don't do it. Once you start feeling the limitations of shell scripts, switch to a real programming language! You will thank yourself later. – marcelm Nov 27 '17 at 13:11

4 Answers4

13

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.

muru
  • 193,181
  • 53
  • 473
  • 722
  • 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 04:06
  • @GoldHaloWings If you mean signal handlers, AFAIK QML signal handlers are in JavaScript (with C++ a possibility). See http://doc.qt.io/qt-4.8/qmlevents.html. No bash. – muru Nov 27 '17 at 04: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 04: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.

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • 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 05: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 05:58
  • @GoldHaloWings you're welcome. Also, I've updated my answer – Sergiy Kolodyazhnyy Nov 27 '17 at 06: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 08:44
  • @GoldHaloWings well, it's a tempting idea, considering that I have final exam coming in couple weeks in one class that uses C++ :) But probably I wouldn't trade Python for C++. – Sergiy Kolodyazhnyy Nov 27 '17 at 08:55
  • @GoldHaloWings But if we're talking about exchanging knowledge, and you want to learn Python - feel free to read my answers and source code of all the app indicators I've done on GitHub (there's links in my profile). I'm happy if people find my stuff useful. – Sergiy Kolodyazhnyy Nov 27 '17 at 08:58
  • Ok i will have a look – GoldHaloWings Nov 27 '17 at 09:01
  • 3
    Maybe worth noting that for GTK to function, `gtk_init()` must be first called, then you allocate some resources like `GtkWidget`s. 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 09:29
  • *"Make GUI in python, but let it communicate with a shell script via named pipe or file."* This has been my strategy to-date communicating between bash apps. Lately I've been wondering about using DBUS instead named FIFO pipes because the speed is incredible and pipes have confusing read/block / non-blocking write voodoo incantations. PS started my first Python project this week to manage a years worth of gmail.com messages with attachments that are daily backup tar files encoded as base-64 :) – WinEunuuchs2Unix Nov 10 '18 at 01:12
1

I know this thread is old, still those who are seeking for an answer, there is a program named gtkdialog which might be useful. Its documentation is here and examples are here.

L_R
  • 11
  • 3
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>&-
user285259
  • 252
  • 1
  • 6
  • 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 07:44