首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python)我想获取一份调查并打印结果

python)我想获取一份调查并打印结果
EN

Stack Overflow用户
提问于 2019-05-09 18:16:30
回答 1查看 96关注 0票数 -2
  1. 我想在比赛前调查一下谁会赢。
  2. 弹出框不会关闭,下一段代码也不会运行。
  3. 和结果窗口甚至都不能工作。

代码语言:javascript
复制
import turtle as t
import random
from tkinter import *
from tkinter import messagebox
import os

root = Tk() 
root.title("choice")
root.geometry("300x170")
root.resizable(0,0)

window = Tk() 
window.title("result")
window.geometry("300x170")
window.resizable(0,0)

def quit():
    global root
    root.exit()

t.speed(10)
t.penup()
t.goto(-300,250)

for step in range(10):
    t.write(step,align='center')
    t.right(90)
    t.forward(10)
    t.pendown()

    for line in range(8):
        t.forward(30)
        t.penup()
        t.forward(30)
        t.pendown()

    t.penup()
    t.backward(490)
    t.penup()
    t.left(90)
    t.forward(20)

tu1=t.Turtle()
tu1.color('red')
tu1.shape('turtle')
tu1.penup()
tu1.goto(-300,190)
tu1.pendown()

tu2=t.Turtle()
tu2.color('blue')
tu2.shape('turtle')
tu2.penup()
tu2.goto(-300,130)
tu2.pendown()

tu3=t.Turtle()
tu3.color('green')
tu3.shape('turtle')
tu3.penup()
tu3.goto(-300,70)
tu3.pendown()

def your_choice():
    yn = 'not selected'

    if Radiovar.get() == 1:
        yn = "no.1 is selected"
    elif Radiovar.get() == 2:
        yn = "no.2 is selected"
    elif Radiovar.get() == 3:
        yn = "no.3 is selected" 

    lbl2.configure(text="your choice: "+yn)
    messagebox.showinfo("your choice",yn)

lbl = Label(root, text="""Which turtle do you think will win?""", font="NanumGothic 10")
lbl.pack()

yn = StringVar()

Radiovar = IntVar()

Radio_button1 = Radiobutton(text="no.1",variable=Radiovar,value=1)
Radio_button2 = Radiobutton(text="no.2",variable=Radiovar,value=2)
Radio_button3 = Radiobutton(text="no.3",variable=Radiovar,value=3)

btn = Button(root, text="choice",command=your_choice,width=5,height=1)

lbl2 = Label(root,text="your choice : ")

Radio_button1.pack()
Radio_button2.pack()
Radio_button3.pack()

btn.pack()
lbl2.pack()

root.mainloop()
root.quit() 

for go in range(70):
    sp1=tu1.forward(random.randint(1,8))
    sp2=tu2.forward(random.randint(1,8))
    sp3=tu3.forward(random.randint(1,8))

corLab1 = Label(window, text="Correct");
faiLab1 = Label(window, text="fail");

if Radiovar==1:
    if sp1>sp2 and sp1>sp3 :
        corLab1.pack();
    else:
        faiLab1.pack();
elif Radiovar==2:
    if sp2>sp1 and sp2>sp3 :
        corLab1.pack();
    else:
        faiLab1.pack();
else:
    if sp3>sp1 and sp3>sp2 :
        corLab1.pack();
    else:
        faiLab1.pack();
EN

回答 1

Stack Overflow用户

发布于 2019-05-10 03:32:44

你的程序是“魔法思维”的结果,不可能工作。例如:您正在使用独立的turtle,而您应该使用嵌入的turtle;您在初始化代码的中间调用root.mainloop()来将控制权移交给Tk事件循环;import os在这里没有意义;您创建了两个Tk根!;您在用户有机会选择一个turtle之前运行了比赛;您已经在所有地方硬编码了三个turn,即使您的赛道被设计用于更多。

下面是我对你的代码的完全修改,基本上就是让它运行:

代码语言:javascript
复制
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
from random import randint
from tkinter import *

COLORS = ['red', 'green', 'blue', 'magenta']

def run_race():
    selected = COLORS[radio_var.get()]

    label.configure(text="Your choice: " + selected)

    winner = None

    while True:
        for racer in racers:
            if racer.xcor() >= 300:
                winner = racer
                break
            racer.forward(randint(1, 8))

        else: # no break
            continue

        break

    window = Toplevel(root)
    window.title('Guess Result')
    window.geometry('200x60')

    correct = Label(window, text="Correct!")
    failure = Label(window, text="Fail!")

    if winner.pencolor() == selected:
        correct.pack()
    else:
        failure.pack()

root = Tk()
root.title("Turtle OTB")
root.geometry('700x700')

Label(root, text="Which turtle do you think will win?").pack()

radio_var = IntVar()

for index, color in enumerate(COLORS):
    Radiobutton(root, text=color, variable=radio_var, value=index).pack()

Button(root, text="Start Race", command=run_race, width=10, height=1).pack()

label = Label(root, text="Your choice: ")
label.pack()

canvas = ScrolledCanvas(root)
canvas.pack(fill=BOTH, expand=True)
screen = TurtleScreen(canvas)

screen.tracer(False)

t = RawTurtle(screen)
t.penup()
t.goto(-300, 250)

for step in range(0, 11):
    t.write(step, align='center')
    t.right(90)
    t.forward(10)
    t.pendown()

    for line in range(8):
        t.forward(30)
        t.penup()
        t.forward(30)
        t.pendown()

    t.penup()
    t.backward(490)
    t.left(90)
    t.forward(60)

racers = []

for gate, color in enumerate(COLORS):
    racer = RawTurtle(screen, 'turtle')
    racer.speed('fastest')
    racer.color(color)
    racer.penup()
    racer.goto(-300, 195 - gate * 60)
    racer.pendown()

    racers.append(racer)

screen.tracer(True)
screen.mainloop()

然而,它仍然需要改进。(例如,能够在不重新启动程序的情况下运行新的比赛。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56057192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档