tkinter 中的代码停止工作(自我)
Code in tkinter stopped working (self)
我这里有一些代码,但我不知道它有什么问题。它总是给我错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "F:\Intro to programing\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\tkinter\__init__.py", line 1489, in __call__
return self.func(*args)
File "D:/Jacob/Pygrams/X6.py", line 38, in key
Ball.down_ball1()
TypeError: down_ball1() missing 1 required positional argument: 'self'
它在我更改了一些东西之后开始给我这个,但后来又改回来了。它之前运行良好。
from tkinter import *
from random import randint
import time
root = Tk()
class Ball:
def __init__(self, canvas, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.canvas = canvas
self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="orange")
def key(event):
a = str(repr(event.keysym))
if a == "'Up'":
Ball.up_ball1()
elif a == "'Down'":
Ball.down_ball1()
elif a == "'Right'":
Ball.right_ball1()
elif a == "'Left'":
Ball.left_ball1()
def left_ball1(self):
self.canvas.move(self.ball1, -3, 0)
def right_ball1(self):
self.canvas.move(self.ball1, 3, 0)
def stop_ball1(self):
self.canvas.move(self.ball1, 0, 0)
def up_ball1(self):
self.canvas.move(self.ball1, 0, -3)
def down_ball1(self):
self.canvas.move(self.ball1, 0, 3)
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, bg="blue", width = 300, height = 200)
ball1 = Ball(canvas, 10, 10, 30, 30)
v = StringVar()
e = Entry(root,textvariable=v)
e.bind("<Key>", Ball.key)
canvas.pack()
e.pack()
root.mainloop()
这种说法和其他类似的说法是不正确的:
Ball.up_ball1()
您应该将它们更改为:
self.up_ball1()
我这里有一些代码,但我不知道它有什么问题。它总是给我错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "F:\Intro to programing\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\tkinter\__init__.py", line 1489, in __call__
return self.func(*args)
File "D:/Jacob/Pygrams/X6.py", line 38, in key
Ball.down_ball1()
TypeError: down_ball1() missing 1 required positional argument: 'self'
它在我更改了一些东西之后开始给我这个,但后来又改回来了。它之前运行良好。
from tkinter import *
from random import randint
import time
root = Tk()
class Ball:
def __init__(self, canvas, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.canvas = canvas
self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="orange")
def key(event):
a = str(repr(event.keysym))
if a == "'Up'":
Ball.up_ball1()
elif a == "'Down'":
Ball.down_ball1()
elif a == "'Right'":
Ball.right_ball1()
elif a == "'Left'":
Ball.left_ball1()
def left_ball1(self):
self.canvas.move(self.ball1, -3, 0)
def right_ball1(self):
self.canvas.move(self.ball1, 3, 0)
def stop_ball1(self):
self.canvas.move(self.ball1, 0, 0)
def up_ball1(self):
self.canvas.move(self.ball1, 0, -3)
def down_ball1(self):
self.canvas.move(self.ball1, 0, 3)
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, bg="blue", width = 300, height = 200)
ball1 = Ball(canvas, 10, 10, 30, 30)
v = StringVar()
e = Entry(root,textvariable=v)
e.bind("<Key>", Ball.key)
canvas.pack()
e.pack()
root.mainloop()
这种说法和其他类似的说法是不正确的:
Ball.up_ball1()
您应该将它们更改为:
self.up_ball1()