简体   繁体   English

通过Tkinter中的按钮调用变量

[英]Calling a variable through a button in Tkinter

I'm new to Python and I've been reading some things about Tkinter and I find it really interesting; 我是Python新手,我一直在阅读有关Tkinter的一些内容,我发现它非常有趣; that said I have a problem with this code I have. 我说我的代码有问题。 I have this program that makes the computer pick a random number between 1 and 10 and you have to guess the number; 我有这个程序,让计算机选择1到10之间的随机数,你必须猜测数字; yes, really simple; 是的,非常简单; and since no new int is generated after a wrong attempt or a right attempt I want to make a button that generates a new random number when it had been clicked. 并且由于在错误的尝试或正确的尝试之后没有生成新的int我想创建一个按钮,当它被单击时生成一个新的随机数。 I saw some solutions with 'Class' stuff but I'm not really familiar with it yet but if that's the best solution for my problem, well, I gotta see. 我看到了一些“类”的解决方案,但我还不熟悉它,但如果这是我问题的最佳解决方案,那么,我得看看。

from Tkinter import *
import random

root = Tk()

random_number = random.randint(1, 10)


def retrieve_input(event):
    inp = float(number_space.get())
    if inp == random_number:
        print("You got it!")
    else:
        print("That's not the number.")
        print("The number was {}".format(random_number) + '.')



number = Label(root, text="Guess a number between 1 and 10:")
number.grid(row=0)
number_space = Entry(root)
number_space.grid(row=0, column=1)
butt = Button(root, text="Generate a new random number")
butt.grid(row=1, column=0)


number_space.bind("<Return>", retrieve_input)




root.mainloop()

i would restructer some of your code because you are wanting to be able to generate the random number in several places in your code it would be better to put it in a sub then call that sub whenever you want a new random integer and then just return the value. 我会重构你的一些代码,因为你想要能够在你的代码中的几个地方生成随机数,最好将它放在sub中,然后在你想要一个新的随机整数时调用那个sub然后返回价值。

def randomnumgen():
    random_number = random.randint(1, 10)
    return random_number

then when you are calliing the subroutine do the value that you want it to be stored in then = nameofsubroutine 那么当你调用子程序时,你需要将它存储在那个值中= thenofsubroutine

num = randomnumgen()

You don't really need a button to generate a new random number as you can do that when the user enters their answer. 当用户输入答案时,您实际上不需要按钮来生成新的随机数。 The button could just restart the game by clearing the entry box. 按钮可以通过清除输入框重新启动游戏。 So as soon as the user hits Return, a random number is generated at that point, and then the logic checks whether their answer == that new random number. 因此,只要用户点击Return,就会在该点生成一个随机数,然后逻辑检查他们的答案==那个新的随机数。

This would happen every time the user enters something, so it negates the need to have a separate button for number generation 每次用户输入内容时都会发生这种情况,因此无需使用单独的按钮进行数字生成

from Tkinter import *
import random

root = Tk()

def retrieve_input(event):
    random_number = random.randint(1, 10)
    inp = float(number_space.get())
    if inp == random_number:
        print("You got it!")
    else:
        print("That's not the number.")
        print("The number was {}".format(random_number) + '.')

def startAgain():
    number_space.delete(0, 'end')


number = Label(root, text="Guess a number between 1 and 10:")
number.grid(row=0)
number_space = Entry(root)
number_space.grid(row=0, column=1)
butt = Button(root, text="Generate a new random number", command=startAgain)
butt.grid(row=1, column=0)

number_space.bind("<Return>", retrieve_input)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM