user2153984
user2153984

Reputation: 193

tkinter quit crash

I am very new to Tkinter. I made this "Hello World"-like GUI program in Tkinter. However, every time I click on the quit button, the program crashes. Thanks in advance!

from Tkinter import *
import sys
class Application(Frame):

def __init__(self,master=None):

    Frame.__init__(self,master=None)
    self.grid()
    self.createWidgets()

def createWidgets(self):
    self.quitButton = Button(text='Quit',command=self.quit)#Problem here
    self.quitButton.grid()
app = Application()
app.master.title("Sample application")
app.mainloop()

Upvotes: 4

Views: 6645

Answers (5)

user11880533
user11880533

Reputation:

try using raise SystemExit this may be better. or check out me

Upvotes: 0

Guydangerous99
Guydangerous99

Reputation: 145

Your using __init__ difficultly. Do this:

from tkinter import *
root = Tk()

btn_quit = Button(root, text='Quit', command=quit()).pack()
root.mainloop()

If you do self.quit, that is the quit command so the thing will crash! Hope this helps!

Upvotes: 0

Subrata Sarkar
Subrata Sarkar

Reputation: 72

When you use self.quit() the python interpreter closes down without the tkinter application bieng closed . So try .destroy() command and after .mainloop() use sys.quit(). Hope this helps.

Upvotes: 0

Philipp Klimaax
Philipp Klimaax

Reputation: 23

This Code works fine now:

import tkinter

class MyApp(tkinter.LabelFrame):
    def __init__(self, master=None):
        super().__init__(master, text="Hallo")
        self.pack(expand=1, fill="both")
        self.createWidgets()
        self.createBindings()
    def createWidgets(self):
        self.label = tkinter.Label(self)
        self.label.pack()
        self.label["text"] = "Bitte sende ein Event"
        self.entry = tkinter.Entry(self)
        self.entry.pack()
        self.ok = tkinter.Button(self)
        self.ok.pack()
        self.ok["text"] = "Beenden"
        self.ok["command"] = self.master.destroy
    def createBindings(self):
        self.entry.bind("Entenhausen", self.eventEntenhausen)
        self.entry.bind("<ButtonPress-1>", self.eventMouseClick)
        self.entry.bind("<MouseWheel>", self.eventMouseWheel)
    def eventEntenhausen(self, event):
        self.label["text"] = "Sie kennen das geheime Passwort!"
    def eventMouseClick(self, event):
        self.label["text"] = "Mausklick an Position " \
        "({},{})".format(event.x, event.y)
    def eventMouseWheel(self, event):
        if event.delta < 0:
            self.label["text"] = "Bitte bewegen Sie das Mausrad"\
            " in die richtige Richtung."
        else:
            self.label["text"] = "Vielen Dank!"

root = tkinter.Tk()
app = MyApp(root)
app.mainloop()

Upvotes: 0

A. Rodas
A. Rodas

Reputation: 20679

In Tkinter the root element is a Tk object. Application should be a subclass of Tk, not Frame:

from Tkinter import *
import sys

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.quitButton = Button(text='Quit',command=self.destroy) # Use destroy instead of quit
        self.quitButton.grid()

app = Application()
app.title("Sample application")
app.mainloop()

Upvotes: 4

Related Questions