2017-09-23 2 views
-1

Je le code suivant, et l'erreur est que 1 argument de position (texte) est manquant. Pour autant que je puisse voir, j'ai inclus tous les arguments. Quelqu'un peut-il nous éclairer à ce sujet?texte ne s'affiche dans Tkinter

Erreur

TypeError: draw_text() missing 1 required positional argument: 'text' 

code en question

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() 

classe dans laquelle draw_text est définie.

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) 

Nouveau à Tkinter et après avoir fait beaucoup de recherches, je ne peux toujours pas trouver des détails sur l'obtention que cela fonctionne.

J'ai aussi essayé ceci:

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() 

... avec ce qui suit dans la classe Ball

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() 

cela ne fonctionne pas non plus:

Erreur: Toile n'est pas défini

exemple Runnable (code tout)

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() 

mise à jour finale:

La dernière chose que j'ai essayé est d'ajouter une initialisation méthode comme suit:

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) 

et

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() 

Maintenant, la première erreur (sur les pas de toile ou aucun argument de position) est parti, mais l'écran est pendante.

+0

J'ai essayé ça: game_over (canvas) ..et def game_over (canvas): ..mais ça ne marche toujours pas. Êtes-vous capable de poster comme réponse? – MissComputing

+0

Vous avez 'canvas' comme argument dans' draw_text', et ensuite ne le passez jamais. Cela signifie que 300 -> canvas, 200 -> x, 'Goodbye' -> y, donc vous ne passez rien à 'text', et recevez cette erreur. – SneakyTurtle

+0

Merci SneakyTurtle et abccd - juste poster le code entier – MissComputing

Répondre

1

L'écran se bloque dans votre dernière mise à jour, car la boucle while est toujours en cours d'exécution, vous n'avez pas/sortir de celui-ci. Vous devriez break sortir de la boucle while une fois que votre condition est remplie. semblent

Votre right_turn et left_turn méthodes ne pas fonctionner correctement, mais je vais laisser cela pour vous fixer:

Voici votre code avec quelques autres correctifs.

from tkinter import * 

import random 
import time 

class Game: 
    def __init__(self, canvas): 
     self.canvas=canvas 

    def game_loop(self): ##No need to pass canvas since you are using the same canvas in __init__ 
     if hit_bottom==True: 
      self.draw_text(300,200,'You Lose') 
    def draw_text(self, x, y, text, size='40'): ##No need to pass canvas 
     font=('Helvetica',size) 
     print "Ok" 
     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=self.canvas.create_oval(30,30,50,50,fill=color) ## self.canvas 
     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=self.canvas.create_rectangle(0,0,100,10,fill=color) ## self.canvas 
     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(canvas) 
      #def draw_text(self,canvas,x,y,text,size='40'): 
      draw1.draw_text(300,200,'Goodbye') 
      break ## This stops the while loop 

     time.sleep(0.02) 
    tk.mainloop() # Keep the main window open 
main() 
0

Après avoir fait un certain nombre de changements, avec la plupart sinon tous indiqué des commentaires, voici une version exécutable de votre script qui affiche le texte à la fin du jeu.

from tkinter import * 

import random 
import time 

class Game: 
    def __init__(self, canvas): # Added method. 
     self.canvas=canvas 
    def game_loop(self): # Removed canvas parameter. 
     if hit_bottom==True: 
      self.draw_text(self.canvas,300,200,'You Lose') # Added self.canvas arg 
    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 
      ball1.draw() 
      bat1.draw() 
      tk.update() # Allow screen to be updated (moved here after draw calls). 
     else: 
      draw1=Game(canvas) # Added argument for new __init__() method. 
      draw1.draw_text(canvas,300,200,'Goodbye') # Added canvas arg. 
      tk.update() # Allow screen to be updated. 
      canvas.after(3000) # Added. Pause for a few seconds. 
      return # Terminate. 

#  time.sleep(0.02) # Shouldn't call sleep() in tkinter app. 
     canvas.after(2) # Use universal 'after()` method instead. Time in millisecs. 
main()