I wrote a program to Open a video in GUI format by Python on Ubuntu and I don't want command terminal screen which always open just after program execution through Geany compiler.
Can you please me to get away from this Terminal screen
import Tkinter,ttk
import tkMessageBox
from Tkinter import *
import io,sys,os,subprocess
from tkFileDialog import askopenfilename
def askforvideo():
global process
name= askopenfilename(title=[("videopen")],filetypes=[("Video Files","*.h264")])
if name != "":
subprocess.call(['vlc',name,'--play-and-exit'])
return
root = Tk()
root.title("Flight Recording Application")
mainframe = ttk.Frame(root, padding="200 200 200 200")
mainframe.grid()
ttk.Button(mainframe, text="Video Open", command=askforvideo).grid(column=10, row=3)
ttk.Button(mainframe, text="Exit", command=root.quit).grid(column=95, row=3)
root.mainloop()
The following will work.
Click Edit>Preferences>Tools
In the terminal field change the value to
bash -x %c
This will execute your python command from bash instead of the terminal.
If you are using an older version of Geany, it may be worth updating the repository and using the virtual terminal emulator at the bottom.
Related
Im using:
python 3
ubuntu 16
tkinter
nemo as file manager
I write this code:
#!/usr/bin/python
# coding=utf-8
#! python3
import tkinter as tk
# importa la libreria
mi_ventanita = tk.Tk()
# crea la ventanita
mi_ventanita.geometry("200x500")
mi_ventanita.title("Hola")
mi_frame = tk.Frame(mi_ventanita)
mi_frame.pack()
button = tk.Button(mi_frame, text='Okay', command=quit)
button.pack(side=tk.LEFT)
#Width x Height
mi_ventanita.mainloop()
And saved it in a file namede crear.py
I changed the file permissions from command line with this command:
sudo chmod a+x *.py
When in nemo I double click the file, appears a message:
I choose run in terminal
For one second appear a window border, but then dissapear...
If I run the code from terminal using:
python3 crear.py
The window appears!
If i debug the file using pdb:
python3 -m pdb crear.py
The window appears!
So my question is:
It is possible to open the window when I double click the file from nemo?
I have other python programs, which not use tinker, and work perfectly on double click using nemo as file manager.
Thank you in advance
I am not familiar with Linux of Ubuntu, but I see 2 problems:
button = tk.Button(mi_frame, text='Okay', command=quit)
The quit has to be in quotation marks
button = tk.Button(mi_frame, text='Okay', command="quit")
Commands are not built in, meaning when you pass a parameter in the command option, you are actually executing a function when the button is clicked
def quit():
return
button = tk.Button(mi_frame, text='Okay', command="quit")
A work around:
I created an sh file with this code:
#!/bin/bash
python3 crear.py
Saved and make it executable.
When i double click the sh file, python creates the window...
As acw1688 wrote, the solution was using:
#!/usr/bin/env python3
So the working code is:
#!/usr/bin/env python3
# coding=utf-8
import tkinter as tk
# importa la libreria
mi_ventanita = tk.Tk()
# crea la ventanita
mi_ventanita.geometry("200x500")
mi_ventanita.title("Hola")
mi_frame = tk.Frame(mi_ventanita)
mi_frame.pack()
button = tk.Button(mi_frame, text='Okay', command=quit)
button.pack(side=tk.LEFT)
#Width x Height
mi_ventanita.mainloop()
Thank you very much!
The code below runs properly, both by running on terminal and upon double-clicking the python file :
from Tkinter import *
root = Tk()
im_fr = Label(root)
im_fr.pack(expand = "yes")
root.mainloop()
But, the moment I use PhotoImage for displaying an image, it ceases to run by double clicking the file, nothing happens on a double-click. It runs only through terminal. The code is below:
from Tkinter import *
root = Tk()
img = PhotoImage(file='img.png')
im_fr = Label(root, image = img)
im_fr.pack(expand = "yes")
root.mainloop()
#Runs well on terminal -> python fi.py
#Doesn't run on double-click, nothing happens
All other Python files run well upon being double-clicked, it's just this one which refuses to.
Using python 2.7 on Mint 18.3.
I'm using jupyter notebook on mac, recently I need to write interactive dialog box, so after google, I use Tkinter to make an interactive window.
But I was bothered by this problem couples day ,and still can't find a solution way.
Fisrt example:
from Tkinter import *
from tkFileDialog import askopenfilename
import sys
import os,time
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Second example:
from Tkinter import *
import sys,os
class YourApp(Tk):
def quit_and_close(self):
app.quit()
#os._exit(0)
#sys.exit(1)
#exit(0)
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
And the third one:
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
After running those above code, always need have to force quit python launcher.
It is very weird, and annoying because after forcing quit, I will got the error:
Is it necessary to use python launcher as default window?
Is there possible to set another window to open ?
or is there proper way to close the launcher without causing programming crash?
p.s Even I try to use wxpython, it still open python launcher and got the same problem.
I tried to find out a solution for my problem, but I couldn't find one.
I am using Python27 on Windows 7.
I have an easy Tkinter GUI with a button:
import Tkinter
import sys
def close_window():
root.destroy()
sys.exit()
root = Tkinter.Tk()
#exit button
draw_button = Tkinter.Button(root, text="Quit", command = close_window)
draw_button.grid(row=1, column=1)
root.mainloop()
Now if I use the Quit button the program closes and there is not task left of the program. The problem is if someone uses the X-Button to close the Windows or for examples uses Alt+F4, the task is still running.
For later use I freeze the script to make an executable and if someone uses some method to close the program except the Quit button the task is still running. And if the task is still running he or she can't open the program again, because it is still running in the background and Windows raise an error that the program is still running.
I tried to add some commands after the mainloop but they are all ignored. How can I solve this problem?
Thanks for your help! Max
What about using WM_DELETE_WINDOW. For example:
import tkinter
import sys
def close_window():
root.destroy()
sys.exit()
def win_deleted():
print("closed");
close_window();
root = tkinter.Tk()
#exit button
draw_button = tkinter.Button(root, text="Quit", command = close_window)
draw_button.grid(row=1, column=1)
root.protocol("WM_DELETE_WINDOW", win_deleted)
root.mainloop()
This will close app with ctr+F4.
I've a GUI which will perform some functions when the buttons are pressed.
now i want to create a button in the GUI which will call and run a shell script in the background.
how can i achieve this ?
Not sure if your question is about how to call a shell script in Python, or how to make a button in your GUI. If the former, my comment above (recommending some research on subprocess.Popen) is the solution. Otherwise:
# assuming Python3
import tkinter as tk
import subprocess as sub
WINDOW_SIZE = "600x400"
root = tk.Tk()
root.geometry(WINDOW_SIZE)
tk.Button(root, text="Push me!", command=lambda: sub.call('path/to/script')).pack()
Python can run shell scripts using the supbprocess module. In order to run it in the background you can start it from a new thread.
To use the module
import subprocess
...
subprocess.call(['./yourScript.sh'])
For a good python threading resource you can try: How to use threading in Python?
Adding to what #lakesh said, below is the complete script :
import Tkinter
import subprocess
top = Tkinter.Tk()
def helloCallBack():
print "Below is the output from the shell script in terminal"
subprocess.call('./yourscript.sh', shell=True)
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
Please note that the shell script is in the same directory as that of the python script.
If needed, do chmod 777 yourscript.sh
subprocess.call('./yourscript.sh', shell=True)
and import Tkinter and not import tkinter solved the problems I was facing.
Use Tkinter to create a button. For more info, look at this video:http://www.youtube.com/watch?v=Qr60hWFyKHc
Example:
from Tkinter import *
root = Tk()
app = Frame(root)
app.grid()
button1 = Button(app,"Shell Script")
button1.grid()
root.mainloop()
To add the functionality:
change button1 line to:
button1 = Button(app,"Shell Script",command=runShellScript)
def runShellScript():
import subprocess
subprocess.call(['./yourScript.sh'])