2

我已经看过很多与我现在正在制作的帖子相似的帖子,但我无法利用已提供的建议/解决方案。这是我的代码:

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 编码方面非常平均。我意识到,我的代码可能会更有效率,但我只是不知道如何实现这一点,这就是我来这里寻求帮助的原因。如果你们中的任何人想帮忙,我很乐意更多地解释我的代码。

提前致谢, 威尔

4

1 回答 1

3

利用

button.pack_forget()
button.pack()

或者

button.grid_forget()
button.grid()

或者

button.place_forget()
button.place()

复选按钮似乎有Radiobutton(master, command = function)

您可以在函数中使用上述按钮的上述方法。

于 2013-04-09T12:05:35.057 回答