Arthur Barelli
Arthur Barelli

Reputation: 31

'Window' object has no attribute '_tclCommands'

I'm new to Python and am trying to make a code for class that needs either to clear the screen or make new windows, which I have tried both of but failed miserably. But besides that, I have tried to run this code with different modifications, and almost all the time I get the error: 'Window' object has no attribute '_tclCommands'. What does this error mean? Another thing that could be related to this is that I cannot close the window specifically, but instead need to quit out of IDLE, the error saying: 'Window' object has no attribute 'destroy'. What can I do to fix this?

from tkinter import *
import time
import os

background_color = 'SlateGray'
text_background_color = 'DarkViolet'

class Window:
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
        self.configure(background = background_color)

    def create_widgets(self):
        self.button = Button(self, text = "Clear", command = clear_screen())
        self.button.grid(row = 1, column = 1)

    def clear_screen(self):
        self.Frame.grid_forget()



root = Tk()
root.configure(background = background_color)
root.title("Guess Your Birthday")
app = Window(root)
root.mainloop()

Upvotes: 2

Views: 3895

Answers (2)

shuji
shuji

Reputation: 7528

You probably want to inherit from Frame because your class is missing some of the common methods a Tkinter widget has:

    self.grid()
    self.create_widgets()
    self.configure(background = background_color)

grid() and configure() have not yet been implemented in your class, right? So there lies your problem, you need to subclass a Tkinter widget class to have all those nice methods:

    class Window(Frame):

Upvotes: 3

user4171906
user4171906

Reputation:

Idle is written with Tkinter I am told and so running Tkinter programs in Idle can cause problems, i.e. run it from the command line or another IDE. There are many problems with your program and it is easier to create a working program than to go over each error individually. The Effbot site is a good place to start learning Tkinter as it has simple examples for each widget http://effbot.org/tkinterbook/

class Window:
    def __init__(self, master):
        self.fr=Frame(master)
        self.fr.grid()
        self.create_widgets()
        background_color = 'SlateGray'
        text_background_color = 'DarkViolet'
        self.fr.configure(background = background_color)

        self.tl=Toplevel(master)
        Button(self.tl, text = "Remove TopLevel",
               command=self.clear_tl).grid(row = 1, column = 2)

    def create_widgets(self):
        self.button = Button(self.fr, text = "Clear Frame",
                            command=self.clear_screen)
        self.button.grid(row = 1, column = 1)

    def clear_screen(self):
        self.fr.grid_forget()

    def clear_tl(self):
        self.tl.destroy()  ## removes the window

root=Tk()    
##root.configure(background = background_color)
root.title("Guess Your Birthday")
app = Window(root)
root.mainloop()

Upvotes: 0

Related Questions