带有 tkinter gui 的彩票号码生成器和检查器

Posted

技术标签:

【中文标题】带有 tkinter gui 的彩票号码生成器和检查器【英文标题】:Lottery number generator and checker with tkinter gui 【发布时间】:2022-01-21 16:12:00 【问题描述】:

背景:所以我正计划在假期期间为我的家人举办一场密室逃脱游戏。难题的一部分是他们应该在房间里找到一个隐藏的彩票号码,需要将其输入计算机。当输入正确的数字后,他们将收到下一条线索。

我是制作 GUI 的新手,所以我在如何去做时遇到了一些困难。我目前的第一个问题是我无法检索用户在输入字段中输入的信息,然后应该将其与正确的数字进行比较。

到目前为止的代码:

from tkinter import *
import random

Lottery = Tk()
Lottery.geometry('800x360')
Lottery.resizable(0, 0)
Lottery.title("Lottery")

def draw_numbers():
   random_numbers = random.sample(range(1, 31), 6)
   random_numbers.sort()
   text.config(text = random_numbers)

##COMPUTER INPUT
var = StringVar()
var.set("Lottery numbers")
frame1 = Frame(Lottery)
frame1.pack(side=TOP)

label = Label(frame1, textvariable=var, font =("Arial", 24), width=20)
label.pack(side=TOP)

##COMPUTER NUMBERS
button = Button(frame1, text = "Draw numbers", width = 25, command = draw_numbers)
button.pack(padx = 1, pady = 1)

text = Label(frame1, text = random.sample(range(1, 31), 6))
text.pack(padx = 0, pady = 1)

##USER INPUT
frame2 = Frame(Lottery)
var = StringVar()
var.set("Choose 6 numbers between 1 - 30.")
frame2 = Frame(Lottery)
frame2.pack(side=TOP)

label = Label(frame1, textvariable=var, font
=("Arial", 24), width=70)
label.pack(side=TOP)

#USER NUMBERS
frame2.pack(side=TOP)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)
txtDisplay = Entry(frame2, bd=20, insertwidth=1, font=("Arial", 24), justify='center', width=4)
txtDisplay.pack(side=LEFT)

Lottery.mainloop()

问题:

    除非您单击框架,否则不会显示用户可以输入其数字的文本字段。 如何检索在文本字段中输入的信息,以便将其与正确的数字进行比较?

替代方案:如果我无法在 GUI 中创建程序,我想我会求助于使其成为可以在终端中运行的程序,例如:

import time
import random

while True:
    random_numbers = random.sample(range(1, 31), 6)
    random_numbers.sort()
    correct_numbers = [1,3,5,13,16]

    print("Choose 6 numbers between 1-30")
    num1 = int(input("Enter your first number: "))
    num2 = int(input("Enter your second number: "))
    num3 = int(input("Enter your third number: "))
    num4 = int(input("Enter your fourth number is: "))
    num5 = int(input("Enter your fifth number: "))
    print()
    input("Press enter to draw numbers: ")
    print("…")

    guessed_numbers = [num1, num2, num3, num4, num5]
    guessed_numbers.sort()
    if guessed_numbers == correct_numbers:
            print("Your numbers are: " + str(guessed_numbers))
            print("The drawn numbers are…")
            time.sleep(2)
            print(correct_numbers)
            print("Congratulations, all numbers are correct! Your next clue is..")
            break
    else:
            print("Your numbers are: " + str(guessed_numbers))
            print("The drawn numbers are…")
            time.sleep(2)
            print(random_numbers)
            print("Sorry! Better luck next time..")

到目前为止,在我的代码中,我使用了以下来源的帮助: https://pretagteam.com/question/cant-get-the-code-to-work-for-this-random-number-generator-in-tkinter https://github.com/manjurulhoque/python-lottery-number-generator/blob/master/lottery_number_generator.py

【问题讨论】:

使用focus_set() 设置输入焦点,也许您需要定义如何在条目之间切换,例如TAB 键或绑定Return 键,以及在输入完成时验证。选项validatecommand 的条目检查每个输入。设置选项textvariable=variable where variable = tk.StringVar(),通过variable.get()widget.get()获取内容。 感谢您的回复!问题 1 以某种方式自行解决,问题 2 使用 variable.get() 解决。我并不真正了解这些其他功能的作用或如何使用它们,但我会尝试检查它们以了解更多信息。 【参考方案1】:

也许不是最漂亮的解决方案,但我得到了我正在寻找的主要功能。

import random
from tkinter import *
import time

correct_numbers = [1,3,5,13,16,23]
def Lotto_No():
    try:
        inp_num1 = int(txtInput1.get())
        inp_num2 = int(txtInput2.get())
        inp_num3 = int(txtInput3.get())
        inp_num4 = int(txtInput4.get())
        inp_num5 = int(txtInput5.get())
        inp_num6 = int(txtInput6.get())
        inp_numlist = [inp_num1, inp_num2, inp_num3, inp_num4, inp_num5, inp_num6]
        inp_numlist.sort()        
        if inp_numlist == correct_numbers:
            x = correct_numbers[0]
            q = correct_numbers[1]
            w = correct_numbers[2]
            e = correct_numbers[3]
            r = correct_numbers[4]
            t = correct_numbers[5]
            num1.set(x)
            num2.set(q)
            num3.set(w)
            num4.set(e)
            num5.set(r)
            num6.set(t)
            return
        else:
            random_numbers = random.sample(range(1, 41), 6)
            random_numbers.sort()
            
            x = random_numbers[0]
            q = random_numbers[1]
            w = random_numbers[2]
            e = random_numbers[3]
            r = random_numbers[4]
            t = random_numbers[5]
            num1.set(x)
            num2.set(q)
            num3.set(w)
            num4.set(e)
            num5.set(r)
            num6.set(t)
            return
    except Exception as e:
        print(e)


    
def retrieve_numbers():
    try:
        num1 = int(txtInput1.get())
        num2 = int(txtInput2.get())
        num3 = int(txtInput3.get())
        num4 = int(txtInput4.get())
        num5 = int(txtInput5.get())
        num6 = int(txtInput6.get())
        num7 = int(txtOutput1.get())
        num8 = int(txtOutput2.get())
        num9 = int(txtOutput3.get())
        num10 = int(txtOutput4.get())
        num11 = int(txtOutput5.get())
        num12 = int(txtOutput6.get())
        numlist = [num1, num2, num3, num4, num5, num6]
        randlist = [num7, num8, num9, num10, num11, num12]
        numlist.sort()
        randlist.sort()

    except Exception as e:
        print(e)

def output_message():
    try:
        num1 = int(txtInput1.get())
        num2 = int(txtInput2.get())
        num3 = int(txtInput3.get())
        num4 = int(txtInput4.get())
        num5 = int(txtInput5.get())
        num6 = int(txtInput6.get())
        num7 = int(txtOutput1.get())
        num8 = int(txtOutput2.get())
        num9 = int(txtOutput3.get())
        num10 = int(txtOutput4.get())
        num11 = int(txtOutput5.get())
        num12 = int(txtOutput6.get())
        numlist = [num1, num2, num3, num4, num5, num6]
        randlist = [num7, num8, num9, num10, num11, num12]
        numlist.sort()
        randlist.sort()
        print(numlist)
        print(randlist)
        if correct_numbers == numlist:
            message= "Correct!"
            message_text.set(message)
        elif numlist == randlist:
            message = "Correct!! Amazing, you managed to guess the correct answer. "
            message_text.set(message)
        else: 
            message = ["Incorrect! Try again!","Bad luck! Try again!", "Incorrect!", "Incorrect!", "You are just guessing aren't you?"]
            message_text.set(random.choice(message))
    except Exception as e:
        print(e)
        message = "Check that you have entered all fields correctly"
        message_text.set(message)



Lottery = Tk()
Lottery.geometry('800x460')
Lottery.resizable(0, 0)
frame = Frame(Lottery)
frame.pack()

Lottery.title('Room Escape Lottery machine')

num1 = StringVar()
num2 = StringVar()
num3 = StringVar()
num4 = StringVar()
num5 = StringVar()
num6 = StringVar()
message_text = StringVar()

var = StringVar()
var.set("Choose 6 numbers between 1-40.")
frame1 = Frame(Lottery)
frame1.pack(side=TOP)

label = Label(frame1, textvariable=var, font
=("Arial", 28), width=44)
label.pack(side=TOP)

label2 = Label(frame1, textvariable="", width=24)
label2.pack(side=TOP)
label2 = Label(frame1, textvariable="", width=24)
label2.pack(side=TOP)


frame1.pack(side=TOP)
txtInput1 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput1.pack(side=LEFT)
txtInput2 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput2.pack(side=LEFT)
txtInput3 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput3.pack(side=LEFT)
txtInput4 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput4.pack(side=LEFT)
txtInput5 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput5.pack(side=LEFT)
txtInput6 = Entry(frame1, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtInput6.pack(side=LEFT)

#BUTTON
frame3 = Frame(Lottery)
frame3.pack(side=TOP)
button1 = Button(frame3, padx=8, width=20, pady=8, bd=8, font=("Arial", 26), text="Draw lottery numbers", bg="black", fg="white", command=lambda:[Lotto_No(), retrieve_numbers(), output_message()])
button1.pack(side=TOP)

frame2 = Frame(Lottery)
frame2.pack(side=TOP)
txtOutput1 = Entry(frame2, textvariable=num1, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput1.pack(side=LEFT)
txtOutput2 = Entry(frame2, textvariable=num2, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput2.pack(side=LEFT)
txtOutput3 = Entry(frame2, textvariable=num3, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput3.pack(side=LEFT)
txtOutput4 = Entry(frame2, textvariable=num4, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput4.pack(side=LEFT)
txtOutput5 = Entry(frame2, textvariable=num5, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput5.pack(side=LEFT)
txtOutput6 = Entry(frame2, textvariable=num6, state = DISABLED, bd=20, insertwidth=1, font=("Arial", 30), justify='center', width=4)
txtOutput6.pack(side=LEFT)

frame4 = Frame(Lottery)
frame4.pack(side=BOTTOM)
button2 = Entry(frame4, bd=0, textvariable=message_text, insertwidth=50,state = DISABLED,  font=("Arial", 15), justify='center', width=100)

button2.pack(side=TOP)

Lottery.mainloop()

【讨论】:

以上是关于带有 tkinter gui 的彩票号码生成器和检查器的主要内容,如果未能解决你的问题,请参考以下文章

python随机彩票号码生成器游戏

试探算法_随机生成彩票号码

java的彩票

带有 Tkinter 和多个文件的 Python GUI

多处理:带 GUI 的父/子(tkinter)

丢弃 Tkinter!简单配置快速生成超酷炫 GUI!