다음 코드가 있는데 오류는 1 개의 위치 인수 (텍스트)가 누락되었습니다. 내가 볼 수있는 한, 나는 모든 주장을 포함시켰다. 누구든지 이것에 대해 어떤 생각을 밝힐 수 있습니까?Tkinter에 텍스트가 표시되지 않습니다.
오류
TypeError: draw_text() missing 1 required positional argument: 'text'
코드 draw_text가 정의되어있는
while 1:
if ball1.hit_bottom ==False: #this creates a condition - inside the loop it continues to check to see if the ball has hit (or not) the bottom of the screen
tk.update()
ball1.draw()
bat1.draw()
else:
draw1=Game()
draw1.draw_text(300,200,'Goodbye')
time.sleep(0.02)
main()
클래스 질문입니다.
class Game:
def game_loop(self,canvas):
if hit_bottom==True:
self.draw_text(300,200,'You Lose')
def draw_text(self,canvas,x,y,text,size='40'):
font=('Helvetica',size)
return self.canvas.create_text(x,y,text=text,font=font)
새로운 Tkinter의에 대한 연구를 많이 수행 한 후, 난 아직이 작업을하기에 대한 세부 사항을 찾을 수 없습니다.
def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)
if pos[1] <=0:
self.y=6
#we make a change here as well -alter the if statement to see if the ball has hit the bottom (equal or greater than canvas height), if so hit_bottom =True (as there is no more need to bounce the ball if the game is over!)
if pos[3] >=self.canvas_height:
self.hit_bottom = True
game_over()
이 중 하나가 작동하지 않는 볼 클래스에서 다음과 같이 ...
while 1:
if ball1.hit_bottom ==False: #this creates a condition - inside the loop it continues to check to see if the ball has hit (or not) the bottom of the screen
tk.update()
ball1.draw()
bat1.draw()
else:
game_over()
time.sleep(0.02)
main()
:
오류 : 캔버스를
나는 또한이 시도 정의되지 않음
class Game:
def __init__(self,canvas):
self.canvas=canvas
def game_loop(self,canvas):
if hit_bottom==True:
self.draw_text(300,200,'You Lose')
def draw_text(self,canvas,x,y,text,size='40'):
font=('Helvetica',size)
return self.canvas.create_text(x,y,text=text,font=font)
과 :210 의 Runnable 예 (전체 코드)
from tkinter import *
import random
import time
class Game:
def game_loop(self,canvas):
if hit_bottom==True:
self.draw_text(300,200,'You Lose')
def draw_text(self,canvas,x,y,text,size='40'):
font=('Helvetica',size)
return self.canvas.create_text(x,y,text=text,font=font)
class Ball:
def __init__(self,canvas,bat,color):
self.canvas=canvas
self.bat=bat
self.id=canvas.create_oval(30,30,50,50,fill=color)
self.canvas.move(self.id,100,200)
starting_position=[-3,-2,-1,1,2,3]
random.shuffle(starting_position)
self.x = starting_position[0]
self.y = -3
self.canvas_height=self.canvas.winfo_height()
self.canvas_width=self.canvas.winfo_width()
#Add a hit_bottom object variable here..
self.hit_bottom=False #...note we change the main loop at the bottom to include an if function that utilises this hit_bottom object variable
def hit_bat(self,pos):
bat_pos=self.canvas.coords(self.bat.id)
if pos[2] >=bat_pos[0] and pos[0] <=bat_pos[2]:
if pos[3]>=bat_pos[1] and pos[3] <= bat_pos[3]:
return True
return False
def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)
if pos[1] <=0:
self.y=6
#we make a change here as well -alter the if statement to see if the ball has hit the bottom (equal or greater than canvas height), if so hit_bottom =True (as there is no more need to bounce the ball if the game is over!)
if pos[3] >=self.canvas_height:
self.hit_bottom = True
if self.hit_bat(pos) ==True:
self.y=-6
if pos[0] <=0:
self.x=6
if pos[2]>=self.canvas_width:
self.x=-6
class Pongbat():
def __init__(self,canvas,color):
self.canvas=canvas
self.id=canvas.create_rectangle(0,0,100,10,fill=color)
self.canvas.move(self.id,200,300)
self.x=0
self.canvas_width=self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>',self.left_turn)
self.canvas.bind_all('<KeyPress-Right>',self.right_turn)
def draw(self):
self.canvas.move(self.id,self.x,0)
pos=self.canvas.coords(self.id)
if pos[0]<=0:
self.x=0
if pos[2]>=self.canvas_width:
self.x=0
def left_turn(self,evt):
self.x=-10
def right_turn(self,evt):
self.x=10
def main():
tk=Tk()
tk.title("My 21st Century Pong Game")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
tk.update()
bat1=Pongbat(canvas,'red')
ball1=Ball(canvas,bat1, 'green')
while 1:
if ball1.hit_bottom ==False: #this creates a condition - inside the loop it continues to check to see if the ball has hit (or not) the bottom of the screen
tk.update()
ball1.draw()
bat1.draw()
else:
draw1=Game()
draw1.draw_text(300,200,'Goodbye')
time.sleep(0.02)
main()
최종 업데이트 : 다음과 같이
내가 해봤 최근의 일이다는 초기화 방법을 추가
while 1:
if ball1.hit_bottom ==False: #this creates a condition - inside the loop it continues to check to see if the ball has hit (or not) the bottom of the screen
tk.update()
ball1.draw()
bat1.draw()
else:
draw1=Game(canvas)
#def draw_text(self,canvas,x,y,text,size='40'):
draw1.draw_text(canvas,300,200,'Goodbye')
time.sleep(0.02)
main()
이제 첫 번째 오류 (캔버스 없음 또는 위치 인수 없음)가 사라졌지만 화면이 멈 춥니 다.
나는 이것을 시도했다 : game_over (canvas) ..and def game_over (canvas) : ..하지만 여전히 작동하지 않습니다. 대답으로 게시 할 수 있습니까 – MissComputing
'draw_text'에서'canvas'를 인자로 사용하고 절대로 통과시키지 마십시오. 즉, 300 -> canvas, 200 -> x, 'Goodbye'-> y를 의미하므로 'text'에 아무 것도 전달하지 않고이 오류를 수신합니다. – SneakyTurtle
SneakyTurtle 및 abccd 감사합니다. 전체 코드 – MissComputing