Search code examples
pythontkintertkinter-canvastkinter-entrytkinter-text

Tkinter button doesnt work in class method


I'm trying to create some kind of C# terminal and methods using Tkinter in Python. Everything works fine but now I'm unable to create buttons using a method. It throws an error: _tkinter.TclError: unknown option "-commmand". If I create a button outside the method, It works just fine. But I want to use the method. Do you have any idea where could be the problem? WriteLine, Write or changing the size of the window works well.

Console.py [file]:

from tkinter import *
from tkinter import ttk

class __Console:

    def __init__(self):
        self._Title = "Console Application"  
        self.Background = "black"
        self.Icon = "icon.png"  
        self.Width = 300
        self.Height = 300
        self.__row = 0
        self.__column = 0

        self.window = Tk()
        self.window.title(self._Title)
        self.window.config(bg=self.Background)
        self.window.iconphoto(False, PhotoImage(file=self.Icon))

    @property
    def Title(self):
        return self._Title

    @Title.setter
    def Title(self, value):
        if len(value) > 20:
            raise ValueError("Too long title.")
        self.window.title(value)
        self._Title = value

    def Hi(self):
        print("Hi!")

    def WriteLine(self, text, background = "black", foreground = "white"):
        self.__column = 0
        Label(self.window, text = text, bg = background, fg = foreground).place(x=self.__column, y=self.__row)
        self.__row += 20


    def Write(self, text, background = "black", foreground = "white", padding = 30):
        Label(self.window, text = text, bg = background, fg = foreground).place(x=self.__column, y=self.__row)
        self.__column += len(text) + padding


    def WindowSize(self, width, height, resizable = False):
        self.Width = width
        self.Height = height
        self.window.geometry(f"{self.Width}x{self.Height}")
        if not resizable:
            self.window.maxsize(self.Width, self.Height)
            self.window.minsize(self.Width, self.Height)
        else:
            self.window.minsize(10, 50)
            self.window.maxsize(self.window.winfo_screenwidth(), self.window.winfo_screenheight())

    def Button(self):
        Button(self.window, text= "Some button", commmand=self.Hi).place(x = 100, y = 100)

           
Console = __Console()

main.py [file]:

from Console import Console

Console.Title = "Some APP"
Console.WriteLine("Adasda")
Console.Write("Adasda")
Console.Write("Adasda")
Console.Write("Adasda")

Console.Button() #Here the error happens

Btw commmand=self.Hi is fine, you call methods without the brackets in tkinter buttons.


Solution

  • Try replacing commmand with command as there's nothing like commmand (triple m) command=self.Hi