我有两个文件cointoss和flibbell,python程序。我必须将硬币导入 flibbell 中,以便 flibbell 可以继承用户给出的翻转次数。然后,flibbell 将在 tkinter GUI 中刺激多个硬币投掷。cointoss文件必须有一个main(),所以当我尝试在没有if __name__ == "__main__":
的情况下运行cointoss时,它可以工作,但是当我使用类CoinToss()运行cointoss文件时,它给了我错误说TypeError: __init__() missing 1 required positional argument: 'flips'
然后,当我尝试使用继承的cointoss运行 flipbell.py 时,它给了我一个错误,说它无法识别CoinToss初始化变量。
这是硬币文件:
from random import randint
import random
class CoinToss:
def __init__(self,flips):
self.state = 1
self.numb = flips
def __str__(self):
firstline = 'The ball at the start : ball: %d, state : %d, value : %d' % (0,0,self.numb)
return firstline
def flip(self):
rand_value = randint(0, 1)
if rand_value == 1:
self +=1
else:
self -=1
return self
def main():
flips = int(input("give the number of flips: "))
dxp = CoinToss(flips)
print(dxp)
k = 0
value_change = flips
for i in range(1,(flips*2) +1):
flips = value_change
value_change = CoinToss.flip(flips)
print('after: ball: %d, state: %d, value: %d' % (i, k , value_change))
k = k+1
if __name__ == "__main__":
main()
当我单独运行硬币文件时,这是我应该得到的结果:
python cointoss.py
Give the number of flips : 6
The ball at the start : ball : 0, state : 0, value : 3
after flip 1, ball : 0, state : 1, value : 2
after flip 2, ball : 0, state : 2, value : 3
after flip 3, ball : 0, state : 3, value : 2
after flip 4, ball : 0, state : 4, value : 3
after flip 5, ball : 0, state : 5, value : 2
after flip 6, ball : 0, state : 6, value : 1
这是翻转铃文件:
from tkinter import Tk, Canvas, Button, W, E
import random
from RRcointossRR import *
class FlipBell(CoinToss):
"""
GUI to simulate billiard ball movement.
"""
def __init__(self, wdw, increment, delay):
super(FlipBell,self).__init__(num_flip)
"""
Determines the layout of the GUI.
wdw : top level widget, the main window,
dimension : determines the size of the canvas,
increment : step size for a billiard move,
delay : time between updates of canvas.
"""
self.ballList = []
wdw.title('Coin flips and Bell Curve')
self.dim = num_flip # dimension of the canvas
self.inc = increment
self.dly = delay
self.togo = False # state of animation
# initial coordinates of the ball
self.xpt = self.dim//2
self.ypt = 0
self.cnv = Canvas(wdw, width=self.dim,
height=self.dim, bg='white')
self.cnv.grid(row=0, column=0, columnspan=2)
self.bt0 = Button(wdw, text='start',
command=self.start)
self.bt0.grid(row=1, column=0, sticky=W+E)
self.bt1 = Button(wdw, text='stop',
command=self.stop)
self.bt1.grid(row=1, column=1, sticky=W+E)
self.points = {}
self.end_points = {}
self.value = 0
def placecoin(self, xpt, ypt):
self.cnv.create_oval(xpt-1, ypt-1, xpt+1, ypt+1,
width=2, outline='red', fill='red', tags='coin')
def drawball(self,ball):
"""
Draws the ball on the canvas.
"""
ball.x = ball.value[1][self.points[ball]]
ball.y += ball.dy
self.cnv.create_oval(ball.x-1, ball.y-1, ball.x+1, ball.y+1,
width=1, outline='black', fill='red', tags='dot')
if ball.y == self.dim:
print(ball.x)
print(ball.y)
if self.end_points[ball.x]>=1:
print("inside 1")
self.placecoin(ball.x,ball.y-self.end_points[ball.x])
else:
self.placecoin(ball.x,ball.y)
self.end_points[ball.x]+=2
self.ypt = 0
def add(self): # Add a new ball
a=Ball(self.dim)
self.ballList.append(a)
self.points[a] = 0
def animate(self):
"""
Performs the animation.
"""
for i in range(1,(self.dim)+1):
self.end_points[i] = 0
while self.togo:
self.add()
self.cnv.delete('dot')
self.value+=1
for ball in self.ballList:
self.drawball(ball)
self.points[ball]+=1
if self.points[ball] == self.dim:
self.ballList.remove(ball)
self.value = 0
self.cnv.update()
def start(self):
"""
Starts the animation.
"""
self.togo = True
self.animate()
def stop(self):
"""
Stops the animation.
"""
self.togo = False
class Ball(FlipBell):
ball = 0
def __init__(self, dimension):
self.dim = dimension
self.x = (self.dim)//2 # Starting center position
self.y = 0
self.dx = 10 # Move right by default
self.dy = 1# Move down by defaultaa
self.value = CoinToss.main()
Ball.ball += 1
def main():
"""
Defines the dimensions of the canvas
and launches the main event loop.
"""
top = Tk()
# dimension of canvas
increment = 10 # increment for coordinates
delay = 1 # how much sleep before update
num_flips = 3
num_value = dimension//2
FlipBell(top, increment, delay)
top.mainloop()
if __name__ == "__main__":
main()
另外,我需要更改位于类球(FlipBell)中的self.value = CoinToss()中的值:根据CoinToss主,这是正确的方法吗?
你的问题是,由于num_flip
不是全局定义的(它不应该是全局定义的),所以你的super()
方法找不到任何名为 num_flip
的变量。 这个数字需要传递到子类的__init__
,然后传递给父类。 看:
class FlipBell(CoinToss):
"""
GUI to simulate billiard ball movement.
"""
def __init__(self, wdw, increment, delay, num_flip):
super(FlipBell,self).__init__(num_flip)
或更常见和一般:
class FlipBell(CoinToss):
"""
GUI to simulate billiard ball movement.
"""
def __init__(self, wdw, increment, delay, *args, **kwargs):
super(FlipBell,self).__init__(*args, **kwargs)