tk,Tk().widthdraw() all tk windows instead of just root in IDLE - python

When running
tk.Tk().withdraw()
filePath = filedialog.askopenfile(filetypes=[("Comma seperated values",".csv")])
through command line a file select window shows up (what I want to happen), however when running through IDLE nothing shows up how can I fix this?
I am using Python 3.9.7 on Windows 10

Related

Issues with the "subprocess" module in Python 3.9.1 (or 3.9.2) - "subprocess.Popen" doesn't work

When my script has the .pyw extension, the function subprocess.Popen doesn't work, but if I use the .py extension, it works. Actually the extension is not so important, the main point is if I use the terminal or not to run the script, if I don't use it I have the issue, otherwise no issues.
This wird behaviout happens only in my PC with Python 3.9.1 or 3.9.2 (the last version currently available). However I have another PC with Python 3.9.0, and there the issue doesn't exist.
I'll give you an example. here I have two scripts, below is the first one named main_script.pyw:
from tkinter import *
from tkinter import ttk
import sys, subprocess
def MyFunction():
subprocess.Popen(["start", sys.executable, "script.py"], shell=True)
parent=Tk()
parent.geometry("300x250+370+100")
parent.title("Test")
TestButton=ttk.Button(parent, text="start", width=16, command=MyFunction)
TestButton.place(x=10, y=10)
parent.mainloop()
Here is the second one named script.py:
a=input("give me a number: ")
Both of them are placed in the same directory. The user can start the second script using the function subprocess.Popen only if the extension of the main script is .py, otherwise he can't. How can I solve the issue? Is it a BUG?
I attached a GIF too:
UPDATE! 1
I tried to install Python 3.9.0 in a virtual machine, and there I found the same issue described before! so, I can't understand why in my PC, with the same version, the script works. if I type python --version in my terminal the output is Python 3.9.0. Here is another GIF to show you that it works:
In the installation path (C:\Users\USER_NAME\AppData\Local\Programs\Python), I saw two folders, Python39 and Python38-32. Probably the second one is a refuse from an old installation, I don't know, but maybe it helps to make the script work. What do you think?
I just want to run CLI scripts via Tkinter as you saw in the last GIF (without using the terminal of course). How can I reach my goal?
UPDATE! 2
I'm not sure but, maybe it's a Windows issue. I tried to run Google Chrome using the subprocess.Popen(["start", sys.executable, "script.py"], shell=True) instruction (before you have to open the terminal in the Chrome installation folder C:\Program Files\Google\Chrome\Application) and it worked only in my PC where I never have issues, but with the other one Chrome didn't start, and in the terminal I got this output (I use ConEmu as terminal):
>>> subprocess.Popen(["start", sys.executable, "chrome.exe"], shell=True)
<Popen: returncode: None args: ['start', 'C:\\Users\\aquer\\AppData\\Local\\...>
SyntaxError: Non-UTF-8 code starting with '\x83' in file C:\Program Files\Google\Chrome\Application\chrome.exe on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Current directory:
C:\Program Files\Google\Chrome\Application
Command to be executed:
"C:\Users\USER_NAME\AppData\Local\Programs\Python\Python39\python.exe" chrome.exe
ConEmuC: Root process was alive less than 10 sec, ExitCode=1.
Press Enter or Esc to close console...
I tried to open the terminal in Admin mode but, also in this case, Google Chrome didn't start. Where is the issue from your point of viewes?
I think that it is because for .py files Windows uses python.exe and for .pyw files Windows uses pythonw.exe. The problem is that sys.executable has the file location of the program used to start the main python file (python.exe when the extension is .py and pythonw.exe when the extension is .pyw). So the problem is that you are trying to start the .py program using the python executable that is for .pyw file extensions.
In cmd:
>>> import sys
>>> sys.executable
'C:\\Program Files\\Python37\\python.exe'
in IDLE (doesn't have the terminal window):
>>> import sys
>>> sys.executable
'C:\\Program Files\\Python37\\pythonw.exe'
Therefore, you have to decide which program (python.exe or pythonw.exe) you want to use. You can do it based on the file extension.
A simple solution would be to open a command prompt from your main_script.py manually, and execute script.py in there, if you really have to execute it as an extra process. If you do not have to, just add import script at the place you want the content of script.py to be executed, or, even better, wrap the code in script.py into a function, import script.py at the top of main_script.py and call the funcction at the approproate place. For the first solution, there is an example on DataToFish that has almost the same code as you (using tkinter GUI a.o., you have to scroll down a bit to find the example with a GUI). In short, you just have execute cmd /c {sys.executable} script.pyw from main_script.py.

python script working on Pycharm and cmd but not running when double clicking it

I made a keylogger using pynput and it runs perfectly and as expected on pycharm and when I open it in python IDE then run it from there and when I navigate to the script folder using cmd and run it from there also using cmd like so:
python keylogger.py
But when I double click it the console opens and closes which means the program has errors, But it doesn't!. So I spammed the program and I could get the error that the flashing console screen is displaying:
basically it's in line one
from pynput.keyboard import *
ModuleNotFoundError: No module named 'pynput'
yes I installed pynput with pip, yes I have python installed on my system, yes I'm sure that's the error.
I'm windows 10 BTW
what should I do???

python opens new console window instead of printing into cmd

My Python 3 (3.6) opens new black console window for each Python command I run (pip is just the same).
Example: When I create a hello_world.py with a while True: print("hello world") Loop in it and start it from PowerShell via python test.py it will open a new window, print "hello world" in the new window and leave the console I used to run the script empty... (same happens with Cygwin) using CMD it won't even open the Python window and just closes gives me back a new CMD prompt-line.
Even worse when using PyCharm each time a background-task runs (e.g. syntax-check) a black console window opens for ~1 sec. and thus blocks me from writing code.
Example images to show what is happening:
Code used for test.py:
while True:
print("Hello World")
So far I tried:
creating and modifying manifest files for python.exe/pythonw.exe
reinstalling Python3
installing a different Python3 Version
changing the default codepage for CMD/PowerShell
setting PYTHONENCODING and PYTHONIOENCODING to UTF-8
starting PowerShell/Python as admin
//edit:
the Question is: How can I get back the default Python behavior, as seen on every other (Windows) Computer.
I found the solution is just run cmd or powershell as Administrator any other type of user would cause this problem.

How do I open Python IDLE (Shell WIndow) in WIndows 10?

I am just starting to learn Python and I am using Windows 10. I downloaded and installed Python 3.4.3. But everytime I open Python from my Desktop or from C:\Python\python.exe it just opens a black command prompt without any Menu options like File menu, Edit Menu, Format Menu etc. I can't see any colors of the code, it's just black screen with white text. I searched about it on internet and came to know that what I am opening is the Editor winodws and I need to open Shell Window in order to have access to all of those options and features. I can't figure out where is the .exe of Shell Window and with what name is it? Please help me.
P.S. I also tried to open pythonw.exe that was present in the Python folder where it was installed, but nothing opened.
In Windows you will need to right click a .py, and press Edit to edit the file using IDLE. Since the default action of double clicking a .py is executing the file with python on a shell prompt.
To open just IDLE:
Click on that. C:\Python36\Lib\idlelib\idle.bat
Start menu > type IDLE (Python 3.4.3 <bitnum>-bit).
Replace <bitnum> with 32 if 32-bit, otherwise 64.
Example:
IDLE (Python 3.6.2 64-bit)
I agree with one who says:
just type "IDLE" in the start-menu where it says "Type here to search" and press [{ENTER}]
If your using Windows 10 just type in idle where it says: "Type here for search"
My solution to setting options and then invoking Idle on a python script is:
Set optn=blah
...
Set optn=blah
start pythonw C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python36-32\Lib\idlelib\idle.py STFxlate.py
This allows you to setup the environment prior to invoking idle.
This assumes that pythonw is in the current path
For those using Anaconda, type idle on windows search bar ("Run or Execute command"). This probably wont work if you didn't install anaconda with environment variables. You can also go to
Anaconda3 folder > Scripts >idle.exe
and create a shortcut to you desktop.

Keep Python Command window open on Windows

Ok I know this has been asked/answered a bunch of times, but the solutions are not working in my situation. I created a Python script using 2.7 on a headless linux terminal on a Raspberry Pi, it works perfectly in that environment.
The script contains an sqlite3 database. On my Windows 10 machine, if I simply double click the Python script from Desktop it creates the database.db file, but the Python command prompt closes before I can see the output.
Running the Python script from the Windows Command prompt eg 'start C:\Users\User\Desktop\script.py' does exactly the same thing
Using Run and 'cmd /k C:\Users\User\Desktop\script.py' does make the Command window persist, but then it errors on the line opening the sqlite3 database:
Traceback (most recent call last):
File "C:\Users\User\Desktop\script.py", line 91, in <module>
conn = sqlite3.connect("database.db")
sqlite3.OperationalError: unable to open database file
The reason Im on Windows at all is Im trying to build a GUI with Tkinter and my R Pi has no GUI, help appreciated
You need to pass the full path like C:\Users\User\Desktop\database.db.
The reason is when running from 'cmd /k C:\Users\User\Desktop\script.py' then the working directory where the command is called is different and it tries to look database.db in that folder, and gives you the error.
You can check what the present dir is by :
import os
print os.getcwd()

Categories