I started studying Python last Monday. I tried to create a window using tkinter. Pycharm says there are no mistakes but after running the code the window doesn't appear D:
That's my code
from tkinter import *
window = Tk()
window.title("thecharmii")
window.geometry("700x500")
window.config(background = "black")
window.mainloop()
I downloaded the latest Python version, I guess it's 3.10. When I click on Run the window doesn't appear but it looks like the process is still running. Thanks for the help
try this
from tkinter import *
def main():
window = Tk()
window.title("thecharmii")
window.geometry("700x500")
window.config(background = "black")
window.mainloop()
main()
It works completely fine with me.
Try running it in your cmd/powershell/terminal.
If it is running, then it might be that the configurations for the file you are working with in pycharm are messed up.
Related
I keep on encountering problems with Tkinter in spyder and I am unsure why. Repeatedly I'd spent a lot of time trying to fix code in spyder which is fully operational in IDLE. Is there a simple reason for this? Is my code wrong and IDLE is ignoring the incorrect bits while spyder is having an issue. When running the code there is never an error in spyder it just doesn't operate as expected. I will show some code that displays differently in IDLE than it does spyder.
from tkinter import font
import tkinter as tk
root = tk.Tk()
root.title("N Body Simulation")
root.geometry('400x300')
frame = tk.Frame(root)
frame.pack()
# titlefont = font.Font(family="Lucida Grande",size=45)
titlefont = font.Font(family='Helvetica', name='titlefont', size=50, weight='bold')
text_intro = "N-Body Simulation"
title = tk.Label(root, text=text_intro, font=titlefont)
title.place(relx=0.5, rely=0.1, anchor="center")
root.mainloop()
The image on the top is from spyder and the one on the bottom is IDLE. Why does it keep appearing different, and what am I doing wrong. To me, I have done the necessary steps to change the font size and make it bold. Also is there a way to run it like IDLE but though spyder, sorry if that last part is a silly question. Thanks in advance.
In the code below, the first dialog box gets focus immediately, so the user can just type an answer and press enter. In the second one, that doesn't seem to happen when running in Windows. Running Raspbian 9, both windows get focus when they open.
Is there any way I can get both windows to get focus when they open in Windows?
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
root.withdraw()
answer1 = simpledialog.askstring("Test1","This one gets focus when it opens",parent=root)
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
I have watched this question for a few days now hoping someone might shed some light on this issue. I'm running Python 3.6.5 under windows 10 and get the same problem.
I have tried several different things but it seems Microsoft does things their own way. I have finally found a thing that works, but only if you don't hide the root window:
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
#root.withdraw() # This does not work if you hide the root window
root.update_idletasks()
answer1 = simpledialog.askstring("Test1","This one gets focus",parent=root)
root.update_idletasks()
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
I found the following worked (with a small flicker of the root window on destroy()):
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
root.deiconify()
root.destroy()
I would like the following program to quit on <Any-KeyPress> event.
from tkinter import *
root = Tk()
root.overrideredirect(True)
root.bind('<Any-KeyPress>', lambda e: root.destroy())
root.mainloop()
This works fine on Windows OS. However this does not work on Ubuntu unless I remove the line root.overrideredirect(True) from the above code.
Is this the intended behavior ?
Or is there a way whereby I can make my program to work while still using root.overrideredirect(True) ?
Edit
I just saw a similar question here at SO, where Bryan Oakley suggests using root.focus_force() but it does not help.
Edit 2
I used root.attributes('-fullscreen', True) instead of root.overrideredirect(True) as suggested here and that seems to work now.
Try this:
from tkinter import *
root = Tk()
root.bind('<Any-KeyPress>', quit())
root.mainloop()
Assuming that you want the program to quit, keep the code. If you just want to clear the screen, just use root.destroy() rather that quit(). Using root.overrideredirect(True) will NOT work on Ubuntu.
My problem is that my python code is not working when I run it as a .py file. Here is the code:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
There is more code to it than that, but that is the relevant stuff. It works fine when I use the python shell or type it directly into the python console, but when I run it as a .py file, it seems to skip this code and go on to the rest, without displaying a canvas. I am using windows, but I am not sure what version of python I'm using.
I was also using
from * import tkinter
before, with relevant changes to the code and i changed it to try and help fix it. It didn't work :(
You are missing the eventloop at the end:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
# Enter into eventloop <- this will keep
# running your application, until you exit
tk.mainloop()
Only a personal recommendation: don't use tk as a variable name, use app or root or even win/window
This is my code:
from Tkinter import *
app = Tk()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit)
b.pack()
app.mainloop()
When I run this python launcher pops up and when I close the window or press the quit button the window closes and the python idle says its done but python launcher becomes unresponsive and I have to force quit it for python launcher to quit and disappear from the dock.
Is there a command that I have to use to exit the code properly?
You are looking for sys.exit()
Here is an example that works
from tkinter import *
import sys
class YourApp(Tk):
def quit_and_close(self):
app.quit()
sys.exit()
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
I've been looking all over and it seems like this might have been a known bug.
The best response I've gotten is from:
window = tk.Tk()
window.protocol("WM_DELETE_WINDOW", window.destroy)
Which allows you to close the window with the little x. This ends the process in the spyder console window, but not the process in the applications bar on the mac. That must still be force quit.
I tried making a function like this to destroy the window and exit using sys, but this just causes the freeze again.
def CloseProgram():
sys.exit()
window.destroy()
Try using Terminal instead of an IDE. I've found that IDEs are very finicky, and using Terminal properly exits and quits the Python Launcher.