4

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()
user2153984
  • 183
  • 1
  • 3
  • 10

5 Answers5

4

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()
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • Subclassing `Frame` to create an application is unconventional, but not invalid. Tkinter will create a `Tk` parent instance for the `Frame` automatically, and it can be accessed using the `master` attribute as the OP does. – RedFantom Dec 28 '18 at 11:36
  • 1
    While it is possible to retrieve the generated `Tk` instance, it is better to create it explicitly for the sake of readability - specially for such a simple program where a `Frame` widget does not serve any useful purpose. – A. Rodas Dec 28 '18 at 14:38
0

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()
0

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.

0

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!

Guydangerous99
  • 147
  • 1
  • 12
  • An OOP implementation, as the OP is using, is generally more flexible. In addition, using `quit()` in that way will immediately quit the Python interpreter. You want to pass a callable object to the `command=` keyword argument, so `quit` instead of `quit()`. It is also more conventional to use `root.destroy` instead, as it ends the `mainloop` and leaves clean-up code after the mainloop functional. – RedFantom Dec 28 '18 at 11:39
0

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

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – gehbiszumeis Aug 15 '19 at 08:21