Tkinter 按钮在 class 方法中不起作用
Tkinter button doesnt work in class method
我正在尝试在 Python 中使用 Tkinter 创建某种 C# 终端和方法。一切正常,但现在我无法使用方法创建按钮。它抛出一个错误:_tkinter.TclError: unknown option "-commmand".
如果我在方法之外创建一个按钮,它工作得很好。但是我想使用这个方法。你知道问题出在哪里吗? WriteLine, Write
或更改 window 的大小效果很好。
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()
main.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 按钮中调用没有括号的方法。
尝试用 command 替换 command,因为没有什么比 command 更好的了(三米)
command=self.Hi
我正在尝试在 Python 中使用 Tkinter 创建某种 C# 终端和方法。一切正常,但现在我无法使用方法创建按钮。它抛出一个错误:_tkinter.TclError: unknown option "-commmand".
如果我在方法之外创建一个按钮,它工作得很好。但是我想使用这个方法。你知道问题出在哪里吗? WriteLine, Write
或更改 window 的大小效果很好。
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()
main.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 按钮中调用没有括号的方法。
尝试用 command 替换 command,因为没有什么比 command 更好的了(三米)
command=self.Hi