Couldnt get rid of (_tkinter.TclError: bad event type or keysym UP) problem

2024/10/5 14:52:06

I am running a Linux mint for the first time . I tried coding a python problem but for two days I am continiously facing problems due to Linux interface please

This is my code:-

    import turtleimport timeboxsize=200caught=Falsescore=0#function that are called in keypressdef up():mouse.forward(10)checkbound()def left():move.left(45)def right():move.right(45)def back():mouse.backward(10)checkbound()def quitTurtles():window.bye()#stop the mouse from leaving the square set sizedef checkbound():global boxsizeif mouse.xcor()>boxsize:mouse.goto(boxsize, mouse.ycor())if mouse.xcor()<-boxsize:mouse.goto(-boxsize, mouse.ycor())if mouse.ycor()>boxsize:mouse.goto(mouse.xcor(),boxsize)if mouse.ycor()<-boxsize:mouse.goto(mouse.xcor(),-boxsize)#set up screenwindow=turtle.Screen()mouse=turtle.Turtle()cat=turtle.Turtle()mouse.penup()mouse.penup()mouse.goto(100,100)#add key listenerswindow.onkeypress(up ,'UP')window.onkeypress(right ,'left')window.onkeypress(left ,'Right')window.onkeypress(back ,'DOWN')window.onkeypress(quitTurtles, "Escape")difficulty=window.numinput("difficulty","Enter a difficulty from 1 to 5",minval=1,maxval=5)window.listen()#main loop#note how it changes with difficultywhile not caught:cat.setheading(cat.towards(mouse))cat.forward(8+diffficulty)score=score+1if cat.distance(mouse)<5:caught=truetime.sleep(0.2-(0.1*difficulty))window.textinput("GAME OVER","WELL DONE YOU SCORED:"+str(score*difficulty))window.bye()

and this the error

Answer

This code has several problems, many of which will keep it from running correctly:

Substituted move for mouse:

def up():mouse.forward(10)checkbound()def left():move.left(45)

Unnecessary global declaration as boxsize is not assigned:

def checkbound():global boxsize

In code copy-and-paste, didn't change mouse to cat:

mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()
mouse.penup()

The difficulty variable not spelled consistently:

    cat.forward(8+diffficulty)time.sleep(0.2-(0.1*difficulty))

Incorrect case for boolean:

 caught=true

As noted in comments, total inconsistency in key naming case:

window.onkeypress(right ,'left')
window.onkeypress(left ,'Right')
window.onkeypress(back ,'DOWN')

Bigger picture issues are use of sleep() in an event-driven environment and lack of drawn boundaries so player knows the limits. Rather than address these issues one by one in SO questions, let's rework this code to work within the turtle event environment and be playable as a game:

from turtle import Screen, TurtleBOX_SIZE = 600# functions that are called in keypress
def up():mouse.forward(15)checkbound()def left():mouse.left(45)def right():mouse.right(45)def back():mouse.backward(15)checkbound()def checkbound():''' stop the mouse from leaving the square set size '''if mouse.xcor() > BOX_SIZE/2:mouse.goto(BOX_SIZE/2, mouse.ycor())elif mouse.xcor() < -BOX_SIZE/2:mouse.goto(-BOX_SIZE/2, mouse.ycor())if mouse.ycor() > BOX_SIZE/2:mouse.goto(mouse.xcor(), BOX_SIZE/2)elif mouse.ycor() < -BOX_SIZE/2:mouse.goto(mouse.xcor(), -BOX_SIZE/2)def move():global scorecat.setheading(cat.towards(mouse))cat.forward(2 * difficulty)score += 1if cat.distance(mouse) < 5:screen.textinput("GAME OVER", "WELL DONE YOU SCORED: {}".format(score * difficulty))screen.bye()else:screen.ontimer(move, 200 - 100 * difficulty)score = 0# set up screen
screen = Screen()marker = Turtle()
marker.hideturtle()
marker.penup()
marker.goto(-BOX_SIZE/2, -BOX_SIZE/2)
marker.pendown()
for _ in range(4):marker.forward(BOX_SIZE)marker.left(90)difficulty = int(screen.numinput("difficulty", "Enter a difficulty from 1 to 5", minval=1, maxval=5))cat = Turtle()
cat.shapesize(2)
cat.penup()mouse = Turtle()
mouse.penup()
mouse.goto(200, 200)# add key listeners
screen.onkeypress(up, 'Up')
screen.onkeypress(right, 'Left')
screen.onkeypress(left, 'Right')
screen.onkeypress(back, 'Down')
screen.onkeypress(screen.bye, 'Escape')
screen.listen()screen.ontimer(move, 1000)  # give player a chance to move hand from keyboard to mousescreen.mainloop()
https://en.xdnf.cn/q/120487.html

Related Q&A

WMI lib to start windows service remotely

How do I start a service using the WMI library? The code below throws the exception: AttributeError: list object has no attribute StopServiceimport wmi c = wmi.WMI (servername,user=username,password=p…

TutorialsPoint - Flask – SQLAlchemy not working

I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it givesBad Request The browser (or proxy) sent a request that this se…

Implement f-string like magic to pimp Djangos format_html()

I would like to pimp format_html() of Django. It already works quite nicely, but my IDE (PyCharm) thinks the variables are not used and paints them in light-gray color:AFAIK f-strings use some magic re…

Selecting a set of numbers from a list which add up to a given value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

List of even numbers at even number indexes using list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

How do I check if a list of lists exists in a list of lists?

I got a list of lists b and I want to check if they exist in list a which is also a list of lists. Im currently using the following method which is quite time-consuming. Is there a faster way? b = [[…

How do i print out a number triangle in python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Real time clock display in Tkinter

I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.My codefrom tkinter import *import timeroot = Tk()class Clock:…

cffi export python code to dll , how to read image object in @ffi.def_extern()

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file an…

Python 2.7.5 and Python 3.6.5

I have installed Python 3.6.5 however when i type Python it shows Python 2.7.5. Id like to use Python 3.[aravind@aravind05 Python-3.6.5]$ python3 --version Python 3.6.5[aravind@aravind05 Python-3.6.5]$…