不久前我就开始使用类,并且在以下问题上苦苦挣扎。
单击按钮将标签从0更改为1后,就会出现此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\patht\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
TypeError: increment1() missing 1 required positional argument: 'self'
代码:
#modules
import tkinter
from tkinter import *
from PIL import Image, ImageTk
class Item_Set:
def __init__(self, root, text1, text2, text3,x,y, img, img2, img3):
self.canvas = Canvas(width=400, height=130)
self.x = x
self.y = y
#
self.widget1 = Label(root, text=text1, font=("Courier", 10))
self.image1 = PhotoImage(file=img)
self.button_i = Button(root, text="+", fg="blue",font=("Courier", 6), command=Item_Set.increment1)
self.button_d = Button(root, text="-", fg="red",font=("Courier", 6))
self.num1 = 0
self.widget_num1 = Label(root, text=str(self.num1), font=("Courier", 12))
#
self.widget2 = Label(root, text=text2, font=("Courier", 10))
self.image2 = PhotoImage(file=img2)
self.button_i2 = Button(root, text="+", fg="blue",font=("Courier", 6))
self.button_d2 = Button(root, text="-", fg="red",font=("Courier", 6))
self.num2 = 0
self.widget_num2 = Label(root, text=str(self.num2), font=("Courier", 12))
#
self.widget3 = Label(root, text=text3, font=("Courier", 10))
self.image3 = PhotoImage(file=img3)
self.button_i3 = Button(root, text="+", fg="blue",font=("Courier", 6))
self.button_d3 = Button(root, text="-", fg="red",font=("Courier", 6))
self.num3 = 0
self.widget_num3 = Label(root, text=str(self.num3), font=("Courier", 12))
def start(self):
self.canvas.pack(expand=YES, fill=BOTH)
#
self.canvas.create_window(self.x, self.y, window=self.widget1)
self.canvas.create_image(self.x, self.y+34, image=self.image1)
self.canvas.create_window(self.x+15, self.y+65, window=self.button_i)
self.canvas.create_window(self.x-22, self.y+65, window=self.button_d)
self.canvas.create_window(self.x-2, self.y+65, window=self.widget_num1)
#
self.canvas.create_window(self.x + 150, self.y, window=self.widget2)
self.canvas.create_image(self.x+150, self.y+34, image=self.image2)
self.canvas.create_window(self.x+165, self.y+65, window=self.button_i2)
self.canvas.create_window(self.x+128, self.y+65, window=self.button_d2)
self.canvas.create_window(self.x+148, self.y+65, window=self.widget_num2)
#
self.canvas.create_window(self.x + 300, self.y, window=self.widget3)
self.canvas.create_image(self.x+300, self.y+34, image=self.image3)
self.canvas.create_window(self.x+315, self.y+65, window=self.button_i3)
self.canvas.create_window(self.x+278, self.y+65, window=self.button_d3)
self.canvas.create_window(self.x+298, self.y+65, window=self.widget_num3)
def increment1(self):
self.num1 += 1
self.widget_num1.config(text=str(self.num1))
root = Tk()
root.title('7 Days to die->Items')
item_set_1 = Item_Set(root,'bullet', 'blueberries', 'eggs',40,30, 'img1.png', 'img2.png','img3.png')
item_set_1.start()
item_set_2 = Item_Set(root,'bullet', 'blueberries', 'eggs',40,30, 'img1.png', 'img2.png','img3.png')
item_set_2.start()
item_set_3 = Item_Set(root,'bullet', 'blueberries', 'eggs',40,30, 'img1.png', 'img2.png','img3.png')
item_set_3.start()
root.mainloop()
注意:我已经研究过了,但是找不到答案。
答案 0 :(得分:2)
这很简单。更改
self.button_i = Button(root, text="+", fg="blue",font=("Courier", 6), command=Item_Set.increment1)
到
self.button_i = Button(root, text="+", fg="blue",font=("Courier", 6), command=self.increment1)
看看Python中的类和对象,您可以了解self
在这里的含义。