如何在tkinter中为我的“重试”按钮添加功能?

时间:2019-06-25 10:04:07

标签: python tkinter tkinter-entry

我写了一个生成两个数字的代码,要求用户计算其总和并输入结果。程序工作正常,但是我想要添加一个按钮,再次要求用户输入结果并进行验证。我面临的问题是该条目没有被清除,并且另一个窗口在具有相同条目的原始窗口下打开。

import tkinter as tk
import random
window = tk.Tk()
content = tk.StringVar()
def startgame():
    number1 = random.randint(10, 51)
    number2 = random.randint(10, 51)
    sum_of_num = number1 + number2

    def checkresult():
        global content
        i = int(content.get())
        if i == sum_of_num:
            label = tk.Label(window, text = "Correct Result", fg = "blue").pack()
        else:
            label = tk.Label(window, text = "Incorrect result!! Try again.", fg = "red").pack()


    window.title("Number Guessing Game")
    label = tk.Label(window, text = "Let's begin the Quiz", fg = "blue").pack()
    label = tk.Label(window, text = "---------------------", fg = "blue").pack()
    label = tk.Label(window, text = "Given two numbers", fg = "blue").pack()
    label = tk.Label(window, text = number1, fg = "blue").pack()
    label = tk.Label(window, text = number2, fg = "blue").pack()
    label = tk.Label(window, text = "Add these two numbers and enter the result", fg = "blue").pack()

    sum_of_numbers = tk.Entry(window, textvariable = content)
    sum_of_numbers.pack()
    sum_of_numbers.focus_set()

    button_to_check_result = tk.Button(window, text="Check whether answer is correct", fg = "red", command = checkresult).pack()


startgame()    
button_to_try_again = tk.Button(window, text="Try Again !!", fg = "red", command = startgame).pack()
window.mainloop()

我希望输入清除的窗口再次出现。

3 个答案:

答案 0 :(得分:0)

将整个细分定义为函数:

label = tk.Label(window, text = "Let's begin the Quiz", fg = "blue").pack()
label = tk.Label(window, text = "---------------------", fg = "blue").pack()
label = tk.Label(window, text = "Given two numbers", fg = "blue").pack()
label = tk.Label(window, text = number1, fg = "blue").pack()
label = tk.Label(window, text = number2, fg = "blue").pack()
label = tk.Label(window, text = "Add these two numbers and enter the result", fg = "blue").pack()

然后将该功能绑定到您计划实现的“重置”按钮。或者,如果您想在用户每次按下“检查”按钮时进行重置,则可以在checkresult()中调用该函数。

基本上,您要做的是在现有标签上显示新标签。

答案 1 :(得分:0)

首先,我要定义

text_number1 = tk.StringVar()
text_number2 = tk.StringVar()
sum_of_num = 0

然后我将开始游戏的功能更改为

def startgame():
    global sum_of_num
    number1 = random.randint(10, 51)
    text_number1.set(str(number1))
    number2 = random.randint(10, 51)
    text_number2.set(str(number2))
    sum_of_num = number1 + number2

并相应地标记

label_num1 = tk.Label(window, textvar = text_number1, fg = "blue").pack() 
label_num2 = tk.Label(window, textvar = text_number2, fg = "blue").pack() 

接下来您需要重置按钮

button_to_check_reset = tk.Button(window, text="Reset your question", fg = "red", command = new_question).pack()

最后但并非最不重要的new_question函数

def new_question()
    startgame()
    sum_of_numbers.delete(0, 'end')

这是最低要求。

但是,如果您想要一个没有外观和过时标签的程序,则需要在初始化中再添加一个stringvar

answer_string = StringVar()
answer_string.set("Waiting for your answer")

比其他标签紧随其后

answer_lable = tk.Label(window, textvar = answer_string, fg = "blue").pack() 

然后您需要改革检查结果功能

def checkresult():
        i = int(content.get())
        if i == sum_of_num:
            answer_lable['fg'] ="green"
            answer_string.set("Correct, that was not hard right?")
        else:
            answer_lable['fg'] ="red"
            answer_string.set("Wrong, you moron")

对新问题功能进行了一些修改:

def new_question()
    startgame()
    sum_of_numbers.delete(0, 'end')
    answer_lable['fg'] ="blue"
    answer_string.set("Waiting for your answer...")

我希望这会有所帮助,并祝你好运:)

答案 2 :(得分:0)

为了清除窗口,您需要将所有标签打包到一个框架中。为此,您需要首先从tkinter导入ttk:

from tkinter import ttk
fr = ttk.Frame()

现在,所有变量的主值都需要从“窗口”更改为“ fr”。但是,作为一种更简单的方法,我所做的是使用变量“ window”作为框架。所以,

root= tk.Tk()
window = ttk.Frame(root)

现在,要清除框架,可以使用以下任何命令:

window.pack_forget()
window.destroy()

最终修改的代码如下:

import tkinter as tk
import random
from tkinter import ttk
root = tk.Tk()
content = tk.StringVar()
global window
window = ttk.Frame(root)

def startgame():
    global window
    window.pack_forget()
    window = ttk.Frame(root)
    number1 = random.randint(10, 51)
    number2 = random.randint(10, 51)
    sum_of_num = number1 + number2

    def checkresult():
        global content
        i = int(content.get())
        if i == sum_of_num:
            label = tk.Label(window, text = "Correct Result", fg = "blue").pack()
        else:
            label = tk.Label(window, text = "Incorrect result!! Try again.", fg = "red").pack()


    root.title("Number Guessing Game")
    label = tk.Label(window, text = "Let's begin the Quiz", fg = "blue").pack()
    label = tk.Label(window, text = "---------------------", fg = "blue").pack()
    label = tk.Label(window, text = "Given two numbers", fg = "blue").pack()
    label = tk.Label(window, text = number1, fg = "blue").pack()
    label = tk.Label(window, text = number2, fg = "blue").pack()
    label = tk.Label(window, text = "Add these two numbers and enter the result", fg = "blue").pack()

    sum_of_numbers = tk.Entry(window, textvariable = content)
    sum_of_numbers.pack()
    sum_of_numbers.focus_set()

    button_to_check_result = tk.Button(window, text="Check whether answer is correct", fg = "red", command = checkresult).pack()
    window.pack()

startgame()    
button_to_try_again = tk.Button(root, text="Try Again !!", fg = "red", command = startgame).pack()
window.mainloop()