Essentially what I said in the title. I am having some trouble detecting a win with tkinker and would appreciate some guidance.I think my original race may have been a little poorly done as well.
from tkinter.simpledialog import askstring
import random
import time
root = tk.Tk()
import turtle
wn = turtle.Screen()
wn.bgcolor('lightblue')
color1 = askstring('Enter Color', 'Please enter the turtle color')
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color(color1)
color2 = askstring('Enter Color', 'Please enter the turtle color')
t2.color(color2)
t1.shape('turtle')
t2.shape('turtle')
t1.left(90)
t2.left(90)
t1.goto(-100, -100)
t2.goto(100, -100)
finish_1 = turtle.Turtle()
finish_1.penup()
finish_1.goto(-100, 100)
finish_1.pendown()
finish_1.circle(20)
finish_1.hideturtle()
finish_2 = turtle.Turtle()
finish_2.penup()
finish_2.goto(100, 100)
finish_2.pendown()
finish_2.circle(20)
finish_2.hideturtle()
for x in range(100):
t1.fd(random.randrange(10))
t2.fd(random.randrange(10))
if t1.xcor: 100
turtle.write((color1), move=False, align="left", font=("Arial", 72, "normal"))
if t2.xcor: 100
turtle.write((color2), move=False, align="left", font=("Arial", 72, "normal"))
time.sleep(3)```
CodePudding user response:
First, python cant recognize your window because you've imported just a function from tkinter and not the entire library :
from tkinter.simpledialog import askstring
What it does is that you import the function you need (here askstring). You actually need to import the whole library : import tkinter as tk
.
You import tkinter and name it tk. (You have use root = tk.Tk()
).
Then you need to use the askstring function. You'll have to specifie the whole path to the function instead : tk.simpledialog.askstring
.
Here is a link to the official documentation on how to import a library : https://docs.python.org/3/tutorial/modules.html
And here is your whole source code modified :
import tkinter as tk
import random
import time
root = tk.Tk()
import turtle
wn = turtle.Screen()
wn.bgcolor('lightblue')
color1 = tk.simpledialog.askstring('Enter Color', 'Please enter the turtle color')
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color(color1)
color2 = tk.simpledialog.askstring('Enter Color', 'Please enter the turtle color')
t2.color(color2)
t1.shape('turtle')
t2.shape('turtle')
t1.left(90)
t2.left(90)
t1.goto(-100, -100)
t2.goto(100, -100)
finish_1 = turtle.Turtle()
finish_1.penup()
finish_1.goto(-100, 100)
finish_1.pendown()
finish_1.circle(20)
finish_1.hideturtle()
finish_2 = turtle.Turtle()
finish_2.penup()
finish_2.goto(100, 100)
finish_2.pendown()
finish_2.circle(20)
finish_2.hideturtle()
for x in range(100):
t1.fd(random.randrange(10))
t2.fd(random.randrange(10))
if t1.xcor: 100
turtle.write((color1), move=False, align="left", font=("Arial", 72, "normal"))
if t2.xcor: 100
turtle.write((color2), move=False, align="left", font=("Arial", 72, "normal"))
time.sleep(3)
I don't really understand why you want to create a window but anyway.
Have a nice day
CodePudding user response:
Turtle is designed to be used either standalone or embedded in a tkinter program. You've used the standalone API in a tkinter embedded situation. This appears to be the case just to access the tkinter askstring()
method.
We can turn this into a standalone turtle program by substituting the textinput()
method. Here's a rework of your code that does that along with fixing bugs in the winner test:
from turtle import Screen, Turtle
from random import randrange
FONT = ('Arial', 72, 'normal')
screen = Screen()
screen.bgcolor('lightblue')
color1 = screen.textinput("Enter Color", "Please enter the turtle color")
t1 = Turtle()
t1.shape('turtle')
t1.color(color1)
t1.setheading(90)
t1.penup()
t1.goto(-100, -100)
color2 = screen.textinput("Enter Color", "Please enter the turtle color")
t2 = Turtle()
t2.shape('turtle')
t2.color(color2)
t2.setheading(90)
t2.penup()
t2.goto(100, -100)
finish_1 = Turtle()
finish_1.hideturtle()
finish_1.penup()
finish_1.goto(-100, 100)
finish_1.pendown()
finish_1.circle(20)
finish_2 = Turtle()
finish_2.hideturtle()
finish_2.penup()
finish_2.goto(100, 100)
finish_2.pendown()
finish_2.circle(20)
marker = Turtle()
marker.hideturtle()
for _ in range(100):
t1.forward(randrange(10))
if t1.ycor() >= 100:
marker.write(color1, align='center', font=FONT)
break
t2.forward(randrange(10))
if t2.ycor() >= 100:
marker.write(color2, align='center', font=FONT)
break
screen.exitonclick()