按下按钮时在 Python tkinter Entry 中获取输入 [英] Get input in Python tkinter Entry when Button pressed

查看:30
本文介绍了按下按钮时在 Python tkinter Entry 中获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pyhon tkinter 进行猜数字"游戏,但到目前为止我还无法检索到用户的输入.

I am trying to make a 'guess the number' game with Pyhon tkinter but so far I have not been able to retrieve the input from the user.

当按下 b1 时,我如何在 entry 中获取输入?

How can I get the input in entry when b1 is pressed?

我还想向玩家显示较低或较高的消息作为线索,但我不确定我所拥有的是否正确:

I also want to display a lower or higher message as a clue to the player but I am not sure if what I have is right:

import time
import random
import decimal
import tkinter as tk

root = tk.Tk()

randomnum = float(decimal.Decimal(random.randrange(100,10000))/100)
guess = 0

def get(entry):
    guess = entry.get()
    return guess

def main():
    b1 = tk.Button(root, text="Guess", command=get)
    entry = tk.Entry()
    b1.grid(column=1, row=0)
    entry.grid(column=0, row=0)
    root.mainloop()
    print(guess)
    if guess < randomnum:
        l2 = tk.Label(root, text="Higher!")
        l2.grid(column=0, row=2)
    elif guess > randomnum:
        l3 = tk.Label(root, text="Lower!")
        l3.grid(column=0, row=2)


while guess != randomnum:
    main()
l4 = tk.Label(root, text="Well guessed")
time.sleep(10)

推荐答案

这是一个关于猜数字游戏的 tkinter 版本.

Here is a tkinter version on the number guessing game.

whileafter 不使用!

程序检查非法输入(空字符串或单词)并报告错误信息.它还跟踪猜测数字所需的尝试次数,并用一个大红色横幅报告成功.

Program checks for illegal input (empty str or words) and reports error message. It also keeps track of the number of tries required to guess the number and reports success with a big red banner.

我为小部件赋予了更有意义的名称,并使用了 pack 管理器而不是 grid.

I've given more meaningful names to widgets and used pack manager instead of grid.

您可以使用按钮输入您的猜测或只需按回车键.

You can use the button to enter your guess or simply press Return key.

import time
import random
import tkinter as tk

root = tk.Tk()
root.title( "The Number Guessing Game" )

count = guess = 0

label = tk.Label(root, text = "The Number Guessing Game", font = "Helvetica 20 italic")
label.pack(fill = tk.BOTH, expand = True)

def pick_number():
    global randomnum
    label.config( text = "I am tkinking of a Number", fg = "black" )
    randomnum = random.choice( range( 10000 ) )/100
    entry.focus_force()

def main_game(guess):
    global count
    count = count + 1
    entry.delete("0", "end")
    if guess < randomnum:
        label[ "text" ] = "Higher!"
    elif guess > randomnum:
        label[ "text" ] = "Lower!"
    else:
        label.config( text = f"CORRECT! You got it in {count} tries", fg = "red" )
        root.update()
        time.sleep( 4 )
        pick_number()
        count = 0

def get( ev = None ):
    guess = entry.get()
    if len( guess ) > 0 and guess.lower() == guess.upper():
        guess = float( guess )
        main_game( guess )
    else:
        label[ "text" ] = "MUST be A NUMBER"
        entry.delete("0", "end")
               

entry = tk.Entry(root, font = "Helvetica 15 normal")
entry.pack(fill = tk.BOTH, expand = True)
entry.bind("<Return>", get)
b1 = tk.Button(root, text = "Guess", command = get)
b1.pack(fill = tk.BOTH, expand = True)

pick_number()

root.geometry( "470x110" )
root.minsize( 470, 110 )

root.mainloop()

这篇关于按下按钮时在 Python tkinter Entry 中获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆