使用Python Tkinter的硬币翻转GUI

194 阅读4分钟

在本教程中,我们将使用Python Tkinter编码一个带有图形用户界面(GUI)的硬币翻转程序。本教程旨在教你Tkinter 模块的基础知识,这是一个在Python中开发基于GUI程序的伟大模块。

在Python Tkinter中实现翻转硬币的GUI程序

Tkinter是Python的标准GUI库,用于制作基于界面的应用程序。Tkinter与Python相结合,使制作基于GUI的应用程序变得非常容易。

1.安装模块

对于这个程序,我们需要PythonNumPyPillow和Tkinter库,我们可以用pip轻松下载:

pip install numpy
pip install pillow
pip install tk

2.导入模块

安装完模块后,我们可以通过在程序中导入所有模块来开始编写程序:

import numpy as np
from tkinter import *
from PIL import Image, ImageTk

注意:在上面的代码中,'*'表示我们从模块Tkinter中导入所有的东西。

3.为我们的应用程序创建主窗口

首先,我们将使用Tk()对象初始化Tkinter类,并将其分配给'root'变量。所以现在通过使用root我们可以访问Tkinter模块的所有方法。

在第二行代码中,我们指定输出GUI屏幕的窗口尺寸,比如这里我们给了它一个400*400的值(宽度x高度)。

现在我们使用mainloop方法使窗口持久化,这意味着除非我们自己想关闭它,否则窗口将不会关闭。如果我们跳过这行代码,输出屏幕会出现一次并立即关闭:

root = Tk()
root.geometry("500*500")
root.mainloop()

运行上述代码后,我们会得到一个如下所示的窗口。如果你得到一个像这样的窗口,你就一切正常了,可以继续。

4.加载图像

我们将根据程序的结果来显示硬币的图像。如果输出结果是正面,那么它将显示硬币的正面,如果是反面,则显示硬币的反面。

正面和反面的图像都与我们的程序文件保存在同一个目录下。如果不是,在你的情况下,那么你需要把图像文件名和它的位置一起传过来:

#load heads image
load = Image.open("heads.jpg")
heads = ImageTk.PhotoImage(load)

#load tails image
load = Image.open("tails.jpg")
tails = ImageTk.PhotoImage(load)

5.添加一个按钮

现在我们的主窗口已经准备好了,我们需要一个按钮,我们可以按下它来抛出硬币。

通过按下这个按钮,我们只是在调用一个抛硬币的函数。我们可以使用tkinter中的Button 类来创建这个按钮。

b1=Button(root, text="Toss the Coin", font=("Arial", 10), command=tossTheCoin, bg='teal', fg='white', activebackground="lightblue", padx=10, pady=10)
b1.pack()

上面的代码将在我们的主窗口中呈现一个按钮,其文字为Toss the Coin。命令中**,**我们将传递我们的函数名。

注意:我们对要在主窗口中渲染的每个元素都使用pack() 方法。

6.结果的文本字段

现在,我们为翻转硬币的结果制作一个文本字段,采用文本格式。我们通过使用代码这样做:

tfield = Text(root, width=52, height=5)
tfield.pack()

为了在这个文本字段中插入文本,我们使用插入函数,如下所示:

tfield.insert(INSERT, "Click on the Button.. To Flip the Coin and get the result")

现在,对于结果的每一次变化,我们都需要删除之前插入的文本,因此我们将删除文本的方法与点击按钮绑定在一起。

tfield.delete("1.0", "end")

7.实现tossTheCoin()函数

当我们抛出一枚硬币时,它有50%的机会落在头或尾。我们希望在我们的程序中具有这种公平的性质,这样它就可以接近真实的抛掷硬币的场景。为此,我们将使用NumPy模块的二项式方法。

np.random.binomial(1,0.5)

这将返回1或0。因此,我们可以编写if条件语句来检查它是1还是0,并使用config方法渲染相应的头或尾图像。

完整的代码将看起来像这样-

import numpy as np
from tkinter import *
from PIL import Image, ImageTk


def coinFlip():
    result = np.random.binomial(1,0.5)
    tfield.delete("1.0", "end")

    if(result == 1):
        tfield.insert(INSERT, " It's ————> HEADS")
        i.config(image = heads)
        
    else:
        tfield.insert(INSERT, " It's ————> TAILS")
        i.config(image = tails)

root = Tk()
root.title("Python Coin Flip")

#load heads image
load = Image.open("head.png")
heads = ImageTk.PhotoImage(load)

#load tails image
load = Image.open("tail.png")
tails = ImageTk.PhotoImage(load)

i = Label(root, image=heads)
i.pack()

root.geometry("500x500")
b1 = Button(root, text="Toss the Coin", font=("Arial", 10), command=coinFlip, bg='teal', fg='white', activebackground="lightblue", padx=10, pady=10)
b1.pack()

#Text Field for Result
tfield = Text(root, width=52, height=5)
tfield.pack()
tfield.insert(INSERT, "Click on the Button.. To Flip the Coin and get the result")


root.mainloop()

该程序的最终输出:-

我们的代码的输出--点击硬币翻转的按钮时

总结

这就是本教程的内容。希望你已经学得很好,并且使用Python Tkinter轻松地编写了基于GUI的硬币翻转代码,没有任何麻烦。