Batch code:
TITLE %~nx0
python "c:\users\aaa\desktop\coding\python\first project with tkinter\buttomtest.py"
pause
and python code is:
from tkinter import *
window = Label(text="gg")
print("tt")
and this is what I get:
It shows the cmd prompt window but it doesn't show the window of tkinter
Tkinter is a python module meant ONLY for python.
Now I see what you tried to do but learning python may be the way there is an OS module import OS which could allow you to run command prompt commands. os.system("command here") will allow you to run commands from the system DOS.
Hope this helps and enjoy the community!
You may also run a start command
start File.py
Exit
way to fix that: window.mainloop()
and there is no need for import OS
Related
I've created a little Python app, and I want it to hide the console window in the middle of the process, so renaming it as .pyw won't solve the problem.
It would be best to have some kind of function to minimize the window, any thoughts?
On windows you may use win32api:
from win32 import win32api
from win32 import win32process
from win32 import win32gui
def callback(hwnd, pid):
if win32process.GetWindowThreadProcessId(hwnd)[1] == pid:
# hide window
win32gui.ShowWindow(hwnd, 0)
# find hwnd of parent process, which is the cmd.exe window
win32gui.EnumWindows(callback, os.getppid())
there is a good way to do it with cmd.
open command prompt:
start /min py -x path\test.py
replace your version of python by 'x' and replace 'path' by the real path of your python project. this may help you to never see the console.
you can start your program again in python like this:
import os
os.sysyem('start /min %~dp0test.py)
but I don't know the way to minimize the console in the middle of program.
Much simpler method is to rename the main python source file's extension from py to pyw. This signals python that no console is required and it is a window'ed script.
E.g. rename PythonApp.py to PythonApp.pyw
Haven't tested but should work on all platforms.
How do I run a .py file outside of IDLE in order to use sys.exit? I'm using command=sys.exit and seems to crash inside of tcl & tk when the button is pushed. The exit is working cause it returns SystemExit in the Shell. I've found that I need to run this outside the IDLE to have this work properly. Oh, I'm working from the book "Programming Python" tutorial. How do I run a .py(GUI) file outside of IDLE to see sys.exit work?
import sys
from tkinter import *
widget = Button(None, text='Hello widget world', command=sys.exit)
widget.pack()
widget.mainloop()
1.- In windows double click on the file.
2.- For linux, if not already there, add a shebang first, telling where python is.
Shebangs are like:
#!/usr/bin/python
or
#!/usr/bin/env python
also remember the dot-slash if you call the program from console:
./myscript.py
3.- In OSX follow same as in linux. Also see these specific instructions and these other ones
I have a tkinter script, which runs just fine in IDLE. However, when I double click the .py-file from Windows Explorer, the console window flashes half a second and then it exits.
I was able to screenprint the console window. It says:
...etc.etc...
NameError: global name 'simpledialog' is not defined
simpledialog is a module in tkinter which I use in my script. As I do from tkinter import *, there is no need to explicitly write tkinter.simpledialog.
It works in IDLE, why not as .py?
IDLE uses Tkinter as its graphical environment. It is possible that your code is relying on a side effect of an import by IDLE itself. This is especially true if you use IDLE without a subprocess.
The simpledialog module does not import when using from tkinter import *.
Try adding this to your code:
import tkinter.simpledialog as simpledialog
Have you updated your PATH environment variable so that your Python executable is found? You can find more information on how to do here - Using Python on Windows
But you basically need to make sure that the folder containing python.exe (e.g. C:\Python32) is displayed when you type the following command from a prompt:
echo %PATH%
I had exactly the same problem with one of my scripts utilizing Tkinter.
Adding call to mainloop() fixed the issue.
See this tutorial for an example: [http://sebsauvage.net/python/gui/#import1
In my case, in the init function I have
def __init__(self,Width=400, Height=400):
# Create GUI window ------------------------------
win = Tk()
...
in the end of init I added:
win.mainloop()
Now it works by just running the file.
Hope this helps
Similar trouble for me just now, in my first week with python. But I dimly remembered a similar problem with a simple early test script and thought the trouble then was # comments.
So I tried that with my Tkinter infused .py script. It ran fine in IDLE as you say, then only flashed when clicked in windows. But there were a couple # commented lines at the top of file.
I took them all out and it now runs no sweat directly in windows. Have a look .. for #.
Sorry, can't seem to delete this post. Now the files work #comments included. Don't know what's up with that. ..
I found that changing the executable py file to a file.pyw fixed the problem. This tells python to execute it using the pythonw.exe which runs the script without the terminal/console in the background.
Not sure why this works, perhaps some screwed up environment variables from a previous python installation.
Changing the file's extension to pyw instead of py might solve the problem
Hi i was wondering if you could have the comand prompt box pop into the Tkinter window when you start the program? Some thing like:
from Tkinter import *
admin = Tk()
cmd = Cmd(admin)
cmd.pack()
admin.mainloop()
I'm on windows
http://tkinter.unpythonic.net/wiki/CmdTkHere is what you want, this is not just opening a cmd window. its embedding cmd.exe into a Tkinter.Frame. And a note here if you rename the python script to ".pyw" extension, the console will be hidden. Except for in a virtual environment's.
I don't believe there is any built in console widget. It may be possible to whip up a custom one using the Tkinter Text widget. However, that would take a bit of effort/time.
Another possible option is simply have your program launch command prompt.
Two different ways to launch command prompt on a Windows machine.
import subprocess, os
subprocess.Popen('cmd.exe')
os.system("cmd.exe")
EDIT:
Unforunetly I don't believe there is any built in widget like that. However I thought of another possible solution, check out the code for the IDLE GUI, it has a console and the GUI portion is entirley written using Tkinter. So you may be able to utelize that code.
I have the following two quesions:
1 I want to be able to shutdown windows xp from python code. I am able to do that by running the following code in the python console:
import os
os.system("shutdown -s -f")
But, if i put the same code in a .py file and try to execute it,it does not work. I get the help prompt for the shutdown command.Any way to fix this ?
2 Is there any way i can take a screenshot of the current screen using python on windows ?
Thank You
There's some code to shutdown windows in this message from the python-win32 list.
You can take a screen shot using PIL's ImageGrab module.