Joey B
Joey B

Reputation: 125

Bash script to run Python Tkinter GUI

I am trying to write a Bash script so that I can run my program on a double click. The program uses tkinter and the GUI is the only thing I need to see. My bat file is the following:

python BudgetGUI.py &

This runs the code and successfully prints any print statements I have throughout my code but it never opens up the GUI. It simply runs through and closes immediately.

How can I modify the bash script to run the GUI?

Thanks in advance!

Edit Solutions for both mac and pc would be great, though at the moment I am on PC. I am working in Python3.

Upvotes: 0

Views: 2459

Answers (2)

PythonProgrammi
PythonProgrammi

Reputation: 23463

Make the window made with tkinter stay out of IDLE*

if you have

root = Tk()

at the end put

root.mainloop()

if you have another name for that like:

window1 = Tk()

then, at the end put

window1.mainloop()

and so for any name you gave to the istance of Tk()

A little example

import tkinter as tk

class Window:
    root = tk.Tk()
    label = tk.Label(root, text = "Ciao").pack()


app = Window()
app.root.mainloop()

Python 2

The code above is for python 3. For python 2 you just need to change the first line (Tkinter with T, not t)

import Tkinter as tk

Upvotes: 0

Novel
Novel

Reputation: 13729

You need to add a call to mainloop(). I can't say for sure without seeing your code, but probably you need to add root.mainloop() to the bottom.

You don't need a bash or bat file. For your mac, just add a shebang and make the file executable. For Windows, add a shebang and associate the file with py.exe.

If you want to suppress the command line from popping up along with the GUI, rename your file with a .pyw extension.

Upvotes: 1

Related Questions