Tkinter dummy window crashes - python

I'm using tkinter to display a simple yesno messagebox in python 3.2.
The code is as follows:
x = tkinter.messagebox.askyesno("New Process", "New Process:\n" + e[2:-7] + "\n\nKill?")
Althought there is nothing wrong with the code(it functions as I want it to), there is a window in the background that appears and does not respond.
This window will crash after about a few seconds or after killing the host process.
What might cause this?

A couple of things:
It looks like you're not running it as a root window.
root = Tk()
app = Frame(root)
app.grid()
my_example = Label(app, "text")
my_example.grid()
root.mainloop()
You should put it in a bat file with pause and you'll be able to see the error

Related

Tkinter mainloop() not quitting after closing window

This is NOT a duplicate of Python tkinter mainloop not quitting on closing the window
I have an app that builds on tkinter. I observed at sometimes after I close the window using the X button, the code will not execute past the mainloop() line. This happens completely randomly about 10% of chance. Rest of the time, it works like a charm. I would like to ask if there are any way to force it. As I said, the code blocks on the mainloop() line, thus calling sys.exit() after it does not help.
I am using Python 3.9.8.
This is not 100% reproducible, but here is something that might trigger the problem:
from tkinter import *
root = Tk()
Label(root, 'hi').pack()
mainloop()
print('exited')
My first thought is to use root.mainloop() instead of tkinter.mainloop(). This makes a difference if you are using several windows.
That said, I did see this a long time ago on some old OSes. Never figured out the reason, so I just wrote my own quit function, like this:
import tkinter as tk
def _quit():
root.quit()
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", _quit)
tk.Label(root, 'hi').pack()
root.mainloop()
print('exited')

Python Tkinter GUI not responding

I'm running pycharm on the mac and noticed that the GUI would not open when it previously has. It bounces in the dock and then says that it is not responding.
So I wrote a very simple program to test the GUI and it still doesn't work.
from tkinter import *
import random
import time
root = Tk()
root.geometry("600x400")
var = 0
one = Label(root, textvariable=var)
one.pack()
while 1 == 1:
var = random.randint(1, 100)
time.sleep(1)
root.mainloop()
I tried reinstalling python but it doesn't help. Also, for my other program, textvariable wasn't able to work and I can't figure out why.
In order for the GUI to run and be responsive, mainloop has to execute. But your mainloop will never execute, because your while 1 == 1: loop will never finish. If you want to do something every second, remove that loop and use root.after instead.
Your textvariable isn't working because var is an integer. It needs to be a StringVar.

Python Tkinter small window pops up momentarily before main window

Hello I am making a Python Tkinter GUI program but as I was making it I noticed that a small Tkinter window pops up then closes before the main window pops up. It is very distracting and obviously something that you would have in a professional piece of software. Here is an example of the problem:
from tkinter import *
app = Tk()
app.title("My GUI")
app.iconbitmap(r"C:\Program Files (x86)\Notepad++\Files\journalicon.ico")
app.resizable(0,0)
button = Button(text = "Text")
button.pack()
app.mainloop()
The iconbitmap option was something I found from another stack overflow page and used it. If you know of a better option I would appreciate the help. I am quite lost and would really appreciate any answers.
Try this:
app = Tk()
app.title("My GUI")
app.iconbitmap(app, "C:\Program Files (x86)\Notepad++\Files\icon.ico")
app.resizable(0,0)
app.mainloop()
You let tkinter know that the definition for things inside the window have stopped by calling mainloop. I have defined the window for the iconbitmap when it is called, using "(app, .."
Hope this helps!

how to close a tkinter window without terminating the program?

i have a project that i am working on for class and i am using tkinter to build my basic GUI.
when i run the code i have two drop down menus to choose options from. i also want a button to close the window and advance the program to the next GUI window. however i can not get a button to close the window without also causing the program to terminate. here is my code
from tkinter import *
Options_year = ["2014", "2013", "2012", "2011", "2010"]
Options_month = ["January","February", "March", "April","May", "June", "July","August","September","October","November",
"December"]
master = Tk()
variable_year = StringVar(master)
variable_year.set(Options_year[0])
variable_month = StringVar(master)
variable_month.set(Options_month[0])
window = apply(OptionMenu, (master, variable_year) + tuple(Options_year))
window_month = apply(OptionMenu, (master,variable_month) + tuple(Options_month))
window.pack()
window_month.pack()
button = Button(master, text = "Continue", command = master.quit())
#the line above is the button that i want to use to close the window
button.pack()
mainloop()
print (variable_month.get())
print (variable_year.get())
EDIT:
converted this to a frame and used the supplied answer below and got it to work. thanks to every one who helped me
Destroying the gui also destroys tk Variables. I strongly suspect that you omitted the vital information that the program terminates with a exception traceback due to the attempt to access the .get method of the non-longer existent variable_month. The following works fine.
from tkinter import *
root = Tk()
root.mainloop()
print('here')
Use a Toplevel or frame, put the widgets in it, and destroy() it. You can use master.withdraw() or iconify() if you do not want it to show.

Creating a User Interface Using Tkinter (Python)

I looked through a tutorial on using Tkinter and saw that the following code:
>>> from Tkinter import *
>>> win=Tk()
This should produce a box with the title Tk and nothing else. However, when I try this code out no such box appears. I'm not getting any errors so I suspect it's working as intended. Is it possible that there are additional steps I have to take if I'm on a mac?
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
This code runs automatically, however, in the guide it suggests that I use $ python hello1.py to run this code, which doesn't work. Any ideas on why this might be?
However, this larger block does not work:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy() # optional; see description below
The issue seems to have something to do with mainloop but I'm confused because at the same time that earlier block worked just fine with a root.mainloop() part.
Do you run this code in IDLE?
Try above code in terminal (not in IDLE), then it will work as expected.
So if you want to try and run it in Terminal you should follow the steps below
note- 'I find that running programs is Terminal that involve a tkinter Gui will often crash for me, however it may work for you'
1st - Open Terminal
2nd - Type 'python3.4' then press space bar once
3rd - Open a Finder window
4th - Go to where you saved your python file in the Finder window
5th - Once you have located the file in Finder, drag the file into the Terminal window
6th - Press enter, and enjoy your python program.
another note - 'It sounds like you need a better Python IDE, you should try out PyCharm it is a great Python IDE which you can code and run python programs in including tkinter stuff'
You can download PyCharm here https://www.jetbrains.com/pycharm/

Categories