1

I have a question. I thought that a class can be based on an object or previously define class. When I change it into class Application(object): it doesn't work. Can you tell me why it didn't work and why did the code below works or why did class Application(Frame) works? Frame is not a previously define object and not object. Sorry for my English. Here is my code:

# Lazy Buttons
# Demonstrates using a class with tkinter

from tkinter import *

class Application(Frame): #
    """ A GUI application with three buttons. """
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """  Create three buttons that do nothing. """
        # create the first Button
        self.bttn1 = Button(self, text= "I do nothing.")
        self.bttn1.grid()

        # create second button
        self.bttn2 = Button(self)
        self.bttn2.grid()
        self.bttn2.configure(text="Me too!")

        # create third button
        self.bttn3 = Button(self)
        self.bttn3.grid()
        self.bttn3["text"] = "Same here!"

# main
root= Tk()
root.title("Lazy Buttons")
root.geometry("200x85")

app = Application(root)

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
John Cruz
  • 127
  • 8

1 Answers1

6

Frame is a previously defined class. It's part of tkinter, which you imported on your first line.

Your Application class extends Frame, which means that it gets methods from Frame and can do everything that a Tk Frame can do, like show widgets. If you don't extend Frame, and only extend object instead, this will not work.

It might be clearer to replace...

from tkinter import *

with...

import tkinter as tk

and fix your references to Tk's classes (they would become tk.Button, tk.Frame and tk.Tk).

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62