gpt4 book ai didi

python - 使用 Tkinter,如何在选择单选选项时显示按钮,然后在不再选择该单选选项时消失?

转载 作者:行者123 更新时间:2023-12-04 05:02:54 27 4
gpt4 key购买 nike

我已经看到了一些与我现在正在制作的帖子类似的帖子,但是我无法利用所提供的建议/解决方案。这是我的代码:

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

提前致谢,
将要

最佳答案

使用

button.pack_forget()
button.pack()


button.grid_forget()
button.grid()


button.place_forget()
button.place()

复选按钮似乎有 Radiobutton(master, command = function)
您可以在功能中使用上述按钮的上述方法。

关于python - 使用 Tkinter,如何在选择单选选项时显示按钮,然后在不再选择该单选选项时消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com