【问题标题】:Tkinter button doesnt work in class method [closed]Tkinter按钮在类方法中不起作用[关闭]
【发布时间】:2022-01-21 12:17:14
【问题描述】:

我正在尝试在 Python 中使用 Tkinter 创建某种 C# 终端和方法。一切正常,但现在我无法使用方法创建按钮。它抛出一个错误:_tkinter.TclError: unknown option "-commmand". 如果我在方法之外创建一个按钮,它工作得很好。但我想使用该方法。你知道问题出在哪里吗? WriteLine, Write 或更改窗口大小效果很好。

Console.py [文件]:

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

ma​​in.py [文件]:

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

顺便说一句,commmand=self.Hi 很好,您可以在 tkinter 按钮中调用不带括号的方法。

【问题讨论】:

    标签: python tkinter tkinter-canvas tkinter-entry tkinter-text


    【解决方案1】:

    尝试用 command 替换 commmand,因为没有什么像 commmand(三重 m) command=self.Hi

    【讨论】:

    • 等等。真是个愚蠢的错误XD我真的看不出来。耶稣...谢谢你。
    • @UchihaObito 嘿!如果有帮助,为什么不将我的答案标记为已接受?这对我也很有帮助!
    • 是的,我必须等 10 分钟。
    • 哦没问题!!谢谢顺便说一句..
    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多