Input makes tkinter.filedialog.askdirectory() not work on PyCharm - python

I've previously asked a question about why tkinter.filedialog.askdirectory() doesn't work for me a second time in a program (it looks like the computer flickers for a second but the folder-choosing window doesn't pop-up and the program is awaiting input), but no one has been able to replicate my issue.
However, I have now discovered that it doesn't work only if before I call that function I have some kind of regular input() (I can call it as many times as I want as long as the program doesn't have any input before). It's weird because if I run my py file on CMD it works as intended. Are there any suggestions as to how to fix it?
My PyCharm version is the newest. I am on Windows 10 and using Python 3.8, and it worked for my friend who has Windows 10 and Python 3.6 (so it's clearly not a problem with my python version).
Also, for clarification, I have used the usual
root = tk.Tk()
root.withdraw()
before calling the above function.

Related

Tkinter transparentcolor working with .py but not with .pyw

root.attributes("-transparentcolor", "white") won't work with a .pyw file extension
Up until yesterday all my tkinter GUIs have been working like they should but two days ago all of them stopped working like I want them to. The line root.attributes("-transparentcolor", "white") has always made the white color transparent, like it should, but now it doesn't work like it used to. It does work properly if I launch the program with a .py file extension but not with a .pyw. Obviously I don't want a console window to open along with all of my programs so this is not something I could just accept. Like I said, this has worked fine up until yesterday.
For the past day I've been doing all kinds of things to try to fix this, I even uninstalled python completely and reinstalled the latest version, but nothing has changed my problem.
I'm on Windows 10 and python 3.9.1 right now (after the reinstall, can't remember what version I had before)
I didn't do a single thing that could've made this happen, I didn't update or change anything before my problem showed up, I simply started my computer in the morning like always.
Does anyone have any clue on what I could try? Could anyone try to do the same thing and see if it works for you?
Code below creates a tkinter window with a transparent background if saved as a .py file, but creates a tkinter window with white background if saved like a .pyw file.
from tkinter import *
root = Tk()
root.config(bg= 'white')
root.attributes('-transparentcolor', 'white')
root.mainloop()
Update:
Hmm... I've discovered something interesting!
If I start my program through cmd prompt with pythonw my problem still appears, but if I do the same thing with a cmd prompt running as administrator it all works like before, I get a transparent background!
Update 2:
I've set pythonw.exe to always run as administrator, and this solves my problem. But if I do that I always get a uac prompt which I never got before, so I haven't fixed my problem, I've just found a tedious workaround.
Allrighty then... I've found a solution, even though I do not like it. I don't have anything against windows or Microsoft, but I really do not like the Microsoft store. But I have to give in, this does solve my problem:
I went and installed python 3.9 from the Microsoft store. I do not know why I did it. I do not know why it helped. I started my .pyw file with the installed program/app called "Python 3.9 (Windowed)" and badaboom, badabing, my problem for some frickin reason, disappeared...
I don't know if this is a viable solution long term (or at all for that matter) but it solves my problem, and after 2 days of continuously smashing my head against the keyboard and wanting to set my pc on fire, I have to accept this as a solution and move on...
Thank u #acw1668 and #martineau for trying to help, I really appreciate it!
Tl;dr:
Download python from Microsoft store and use that program to start the file

How to persist Python turtle graphics screen

I am learning Python (v 3.7) on a Mac using PyCharm. As I practice using the turtle library, the program runs without error, outputs the correct graphics, but then the graphics screen disappears immediately after the code completes runnning. Adding time.sleep(5) at the end of my program persists it and also shows that the focus changed from PyCharm to a Python program menu (which I can't find or turn on in the hope of keeping running).
When I use Thonny, the output persists, so I can check my work. How can I make it persist in PyCharm?
A well-structured Python turtle program will end with a call to mainloop() or one of its variants (exitonclick(), done()) This turns control over to the underlying tkinter event handler which will keep your window open, awaiting user events. Without this, the program simply ends and the window closes.
You don't need time.sleep() nor input("Press Enter to continue..."). Some Python programming environments clash with mainloop() but even those tend to disable it behind the scenes so the same code works everywhere.
First, do not use PyCharm to test. If you like it, more power to you, but personally, I have had many issues with output and PyCharm. Learn the command line, since you are using Mac. CodeAcacdemy and LinuxJournery have really good resources on that subject. Second, if you are still having issues, please reinstall trutle, Python 3.7 (there were some issues wiht it displaying on Mac), and macOS itself. Have a great day!
https://linuxjourney.com/
https://www.codecademy.com/learn/learn-the-command-line
Problems getting pygame to show anything but a blank screen on Macos Mojave

Start Python in Background

So I have uTorrent set up to run a Python script when a torrent's state is changed so it can sort it. It all works fine except it takes focus from whatever I'm doing and it's crazy annoying.
I run it using sorter.py <arguments>. I'm on Windows 10. What can I do, if anything, to get this to run in the background and not take focus from what I'm doing?
I'm also sorry if this has already been answered but I couldn't find anything that worked.
Running with pythonw (or changing extension to .pyw which is the same) may help.
pythonw.exe doesn't create a console window but I dunno about focus. It doesn't create any windows by default, either, so it shouldn't steal it.

Pygame crashing

I have recently encountered an issue whenever I try and use pygame. The window that comes up hangs and just ends up crashing. I know this program works because I have run it many times before (this also occurs with anything else I try and run in pygame). Strangely, in the command prompt window it asks an input question from a completely unrelated program I was working on last night.
It could be, that you named your file pygame.py, so now, whenever you try to import pygame, it imports from THAT pygame.py file. Maybe delete/rename the file and reinstall pygame would work, i had the same problem a year ago and it worked.
Seems to be a setup problem.
possible solutions:
rename files (as Mooper suggested)
create the smallest possible use for pygame (once without / once with windows popping up), in order to tell us where the error comes up / does not.
look at the code again; seems to me being an error with a neverending loop / overflowing stack / a function not returning after being called.
last possible idea: reinstall pygame

Windows executeable created with Py2exe won't run on another computer

I created an executeable of my Python script. Everything works just fine on my first computer but when I try to run this very same executeable in another computer, it wont work. When I try to run my executeable I see that computer tries to open it, because command prompt window appears but nothing else happens, prompt window will disappear. Problem can't be in computer, because both my computers are 32 bit Windows machines and I have done similar things before but this kind of error I see first time.
I don't want to post my code at first, because it is more than 500 lines long. But I will give some more specifications about my problem.
This program I create is actually advanced version of application i made earlier. And earlier version worked fine in both of my computers. This advanced version, I am working on now, is developed into many standalone Python scripts unlike first one which was only one script. Could this be the source of my problem? Should I do something different when running setup.py now when I have many scripts?
And then there is third version of my application. Executeable of it runs fine on computer where I created it, but in my second computer it wont run either. But this time I get error too. When I run this program in my second computer the opening screen of aplication appears, there are some buttons which are meant to open other scripts, when they are clicked on. But when I click them I get following error: The system cannot find the path specified.
If someone is willing to look into my long code, then I am willing to share it. But again I dont want to post it here.
I am using Python 2.7 and windows 7 in my first computer and windows XP on my second computer.
I would be very grateful if someone points me right direction which helps me to solve my problem.

Categories