Python does not create window with tkinter on doubleclik - python

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!

Related

Removing the py.exe at the background

I am working on tkinter project. While double clicking the python file, the py.exe file is visible in the background which doesn't look good. so is there a way to make the py.exe invisible or resize it.Please help me with your ideas. Thank you
Sample code:
import tkinter.messagebox
from tkinter import ttk
class Demo1:
data = []
def __init__(self, master):
self.master = master
self.label=tkinter.Label(text="Add IP/Hostname")
self.label.pack()
self.t=tkinter.Text(self.master,height=20,width=50)
self.t.pack()
self.button = tkinter.Button(self.master,height=3,width=10, text="OK"
)
self.button.pack()
def main():
root = tkinter.Tk()
app = Demo1(root)
root.mainloop()
if __name__ == '__main__':
main()
Change the .py extension to .pyw. That should suppress the console window.
Note: the .pyw extension should be associated to be opened with pythonw.exe. Normally this is done by the Python installer. If not, select pythonw.exe the first time you click on a .pyw file. That will make the proper association.
Does that work?
import ctypes
ctypes.windll.user64.ShowWindow(ctypes.windll.kernel64.GetConsoleWindow(), 6 )
Otherwise please post an example code.

Tkinter: Why am I not able to run a code by double-clicking the file?

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.

Why always have to force quit python launcher after running Tkinter on Mac?

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.

Disable/Close/Quit/Exit Terminal screen from python on Geany (Ubuntu)

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.

run a shell script from Python GUI

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'])

Categories

Resources