我已经看到了类似于我现在正在制作的一些公平的帖子,但我无法利用已提供的建议/解决方案。这里是我的代码:使用Tkinter时,如何在选择无线电选项时显示按钮,然后在不再选择无线电选项时消失?
from tkinter import *
import random
# Set up the window
app = Tk()
app.title("SUVAT Learning Game")
app.geometry("720x500+400+100")
menubar = Menu(app)
def highscorewindow():
app = Tk()
app.title("High Scores")
app.geometry("720x500+400+100")
# create an area for working
large_field = Text(app)
large_field.pack(side = 'bottom', pady = 10)
# create an area for an answer
small_field = Entry(app)
small_field.pack(side = 'bottom', pady = 10)
# create areas for the terms
# term_entry1 = Entry(app)
# term_entry1.visible = False
# term_entry1.pack(side = 'bottom', pady = 10)
# term_entry1.pi = term_entry1.place_info()
# term_entry1.place_forget()
# term_entry2 = Entry(app)
# term_entry2.visible = False
# term_entry2.pack(side = 'bottom', pady = 10)
# term_entry2.pi = term_entry2.place_info()
# term_entry2.place_forget()
# term_entry3 = Entry(app)
# term_entry3.visible = False
# term_entry3.pack(side = 'bottom', pady = 10)
# term_entry3.pi = term_entry3.place_info()
# term_entry3.place_forget()
def callback():
print(small_field.get())
def toggle():
term_entry1.visible
term_entry1.place(term_entry1.pi)
term_entry1.visible = not term_entry1.visible
# Set up the questions
questionnumber = random.randint(1, 5)
choice = IntVar()
if questionnumber == 1:
Question = Label(app, text = "A ball is thrown in the air at 13 metres per second and reaches 15 metres into the air. \n\n Which equation is the correct one to use? \n\n Calculate the speed of the ball at this point.")
Question.pack(side = 'top', pady = 32)
# create answer 1 radio button
Choice1 = Radiobutton(text = "V = (((2*S - U))/t)", variable = choice, value = 1, command = "")
Choice1.pack(side = 'top', padx = 10)
# create answer 2 radio button
Choice2 = Radiobutton(text = "t = (S/((U + V)/2))", variable = choice, value = 2, command = "")
Choice2.pack(side = 'top', padx = 10)
# create answer 3 radio button
Choice3 = Radiobutton(text = "V = (math.sqrt((U*U) + 2*a*S))", variable = choice, value = 3, command = toggle) ####correct
Choice3.pack(side = 'top', padx = 10)
elif questionnumber == 2:
Question = Label(app, text = "A truck is travelling at 25 metres per second when the brakes are applied. After 10 seconds, the truck comes to a stop. \n\n Which equation is the correct one to use? \n\n Calculate the deceleration of the truck.")
Question.pack(side = 'top', pady = 32)
# create answer 1 radio button
Choice1 = Radiobutton(text = "a = ((V - U)/t)", variable = choice, value = 1, command = "") ####correct
Choice1.pack(side = 'top', padx = 10)
# create answer 2 radio button
Choice2 = Radiobutton(text = "U = (S/(0.5*a*t))", variable = choice, value = 2, command = "")
Choice2.pack(side = 'top', padx = 10)
# create answer 3 radio button
Choice3 = Radiobutton(text = "a = (S - (V*t)/(-0.5*(t*t)))", variable = choice, value = 3, command = "")
Choice3.pack(side = 'top', padx = 10)
elif questionnumber == 3:
Question = Label(app, text = "A tennis ball is thrown in the air. After 20 seconds, the ball reaches its maximum height. \n\n Which equation is the correct one to use? \n\n Calculate the initial speed of the ball.")
Question.pack(side = 'top', pady = 32)
# create answer 1 radio button
Choice1 = Radiobutton(text = "U = (V/a*t)", variable = choice, value = 1, command = "") ####correct
Choice1.pack(side = 'top', padx = 10)
# create answer 2 radio button
Choice2 = Radiobutton(text = "U = (S/(0.5*a*t))", variable = choice, value = 2, command = "")
Choice2.pack(side = 'top', padx = 10)
# create answer 3 radio button
Choice3 = Radiobutton(text = "S = (V*t - 0.5*a*(t*t))", variable = choice, value = 3, command = "")
Choice3.pack(side = 'top', padx = 10)
elif questionnumber == 4:
Question = Label(app, text = "A firework travels upwards after being launched with a speed of 20 metres per second. \n\n Which equation is the correct one to use? \n\n Calculate the greatest height reached by the firework.")
Question.pack(side = 'top', pady = 32)
# create answer 1 radio button
Choice1 = Radiobutton(text = "S = (((V*V)-(U*U)/2*a))", variable = choice, value = 1, command = "") ####correct
Choice1.pack(side = 'top', padx = 10)
# create answer 2 radio button
Choice2 = Radiobutton(text = "S = ((U*t) + 0.5*a*(t*t))", variable = choice, value = 2, command = "")
Choice2.pack(side = 'top', padx = 10)
# create answer 3 radio button
Choice3 = Radiobutton(text = "a = (S/(U*t*0.5*(t*t)))", variable = choice, value = 3, command = "")
Choice3.pack(side = 'top', padx = 10)
elif questionnumber == 5:
Question = Label(app, text = "A well is 75 metres deep. A pebble is dropped from the top, and hits the bottom with a speed of 12 metres per second. \n\n Which equation is the correct one to use? \n\n Calculate the time the pebble takes to travel from top to bottom.")
Question.pack(side = 'top', pady = 32)
# create answer 1 radio button
Choice1 = Radiobutton(text = "t = (S/((U + V)/2))", variable = choice, value = 1, command = "")
Choice1.pack(side = 'top', padx = 10)
# create answer 2 radio button
Choice2 = Radiobutton(text = "S = ((U*t) + 0.5*a*(t*t))", variable = choice, value = 2, command = "")
Choice2.pack(side = 'top', padx = 10)
# create answer 3 radio button
Choice3 = Radiobutton(text = "t = ((V - U)/a)", variable = choice, value = 3, command = "") ####correct
Choice3.pack(side = 'top', padx = 10)
# create a pulldown menu for scores, and add it to the menu bar
scoremenu = Menu(menubar, tearoff=0)
scoremenu.add_command(label="View Highscores", command=highscorewindow)
scoremenu.add_command(label="Clear Highscores", command=callback)
scoremenu.add_command(label="New Question", command="")
scoremenu.add_separator()
scoremenu.add_command(label="Exit", command=app.quit)
menubar.add_cascade(label="Score Menu", menu=scoremenu)
# create pulldown menu for help
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command="")
menubar.add_cascade(label="Help", menu=helpmenu)
nextbutton = Button(text = "Next", command = highscorewindow)
nextbutton.pack(side = 'top', pady = 10)
nextbutton.position_forget()
# display the menu
app.config(menu=menubar)
mainloop()
正如你可以从我的代码中看到的,我在使用Python编码非常平均。我知道,我的代码可能会更有效率,但我没有掌握这些技巧,这就是我来这里寻求帮助的原因。如果你们中的任何人都喜欢帮忙,我很乐意更多地解释我的代码。
由于提前, 请问
您是否试图编写一个旨在测试各种问题的人的程序? – 2013-04-09 14:49:23
是的,但是稍后会执行程序的其他部分问题。 – WPilcher 2013-04-10 13:56:17
您可能有兴趣查看http://code.activestate.com/recipes/sets/33-quiz-me-25/并在那里修改源代码。您也可以通过发送电子邮件给[email protected]的开发人员获取代码的存档工作版本。 – 2013-04-10 19:28:22