資訊內(nèi)容
Python Tkinter教程 數(shù)字猜謎游戲
KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
Tkinter是Python的Tk GUI(圖形用戶(hù)界面)工具包和事實(shí)上的標(biāo)準(zhǔn)GUI 的標(biāo)準(zhǔn)接口。GUI使您可以使用大多數(shù)操作系統(tǒng)使用的可視項(xiàng)(例如窗口,圖標(biāo)和菜單)與計(jì)算機(jī)進(jìn)行交互。這個(gè)功能強(qiáng)大的工具可用于構(gòu)建各種項(xiàng)目,并且使可視化代碼更加容易。
KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在本文中,我們將了解Tkinter的基礎(chǔ)知識(shí)以及可在Python應(yīng)用程序中使用的不同類(lèi)型的小部件。在本文的后面,我們將使用Tkinter小部件開(kāi)發(fā)一個(gè)很酷的數(shù)字猜測(cè)游戲。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
今天,我們將介紹:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
Tkinter的基礎(chǔ)Tkinter的小部件與示例從頭開(kāi)始構(gòu)建數(shù)字猜謎游戲Tkinter的基礎(chǔ)在構(gòu)建游戲之前,我們需要了解Tkinter的一些基礎(chǔ)知識(shí)。Tkinter軟件包是Tk GUI工具包的標(biāo)準(zhǔn)python接口。我們通常使用Tkinter包在應(yīng)用程序中插入不同的GUI小部件,以使其更加用戶(hù)友好。如果您在Linux,Windows或Mac上使用Python,則設(shè)備上已經(jīng)安裝了Python Tkinter。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
我們?nèi)绾伍_(kāi)發(fā)GUI應(yīng)用程序?創(chuàng)建GUI應(yīng)用程序的基本過(guò)程如下:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
Import the Tkinter ModuleCreate Main WindowAdd WidgetsEnter Main LoopKFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
使用Python開(kāi)發(fā)GUI應(yīng)用程序涉及的步驟:導(dǎo)入tkinter模塊。為我們的GUI應(yīng)用程序創(chuàng)建主窗口。現(xiàn)在,為我們的應(yīng)用程序添加任意數(shù)量的小部件。進(jìn)入主事件循環(huán)以執(zhí)行我們的主要功能。現(xiàn)在讓我們看看如何創(chuàng)建一個(gè)簡(jiǎn)單的tkinter窗口:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
首先,我們將導(dǎo)入tkinter模塊。它包含構(gòu)建應(yīng)用程序所需的所有功能,類(lèi)和其他內(nèi)容?,F(xiàn)在,當(dāng)我們導(dǎo)入模塊時(shí),我們需要初始化tkinter。為此,我們創(chuàng)建Tk( )根窗口小部件?,F(xiàn)在,這將創(chuàng)建我們的主GUI窗口,我們將在其中添加小部件。此時(shí),我們的主窗口只有標(biāo)題欄。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
我們應(yīng)該只為我們的應(yīng)用程序創(chuàng)建一個(gè)窗口,并且必須在添加任何其他小部件之前創(chuàng)建該窗口。之后,我們使用root.mainloop( )。除非輸入,否則不會(huì)顯示我們剛剛創(chuàng)建的主窗口mainloop。當(dāng)我們按下關(guān)閉按鈕時(shí),我們的程序?qū)⑼顺鲋餮h(huán)。在按下關(guān)閉按鈕之前,我們的應(yīng)用程序?qū)⒁恢边\(yùn)行。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
用于創(chuàng)建簡(jiǎn)單的tkinter窗口的代碼:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
#import required libraries from tkinter import * # initialize tkinter : root = Tk() # enter the main Loop : root.mainloop()復(fù)制代碼Tkinter的小部件與示例**按鈕:**顯示按鈕。**畫(huà)布:**繪制形狀。**復(fù)選框:**將多個(gè)選項(xiàng)顯示為復(fù)選框。**輸入:**接受用戶(hù)的單行輸入。**框架:**組織其他小部件。**標(biāo)簽:**為其他小部件添加標(biāo)題。**列表框:**向用戶(hù)提供選項(xiàng)列表。菜單**按鈕:**在我們的應(yīng)用程序中顯示菜單。**菜單:**向用戶(hù)提供各種命令。**消息:**顯示多行文本字段。**單選按鈕:**將選項(xiàng)數(shù)量顯示為單選按鈕。**比例尺:**提供滑塊。**滾動(dòng)條:**添加滾動(dòng)功能。**文字:**以多行顯示文字。**頂層:**提供單獨(dú)的窗口容器。**Spinbox:**從固定輸入值中選擇。**PanedWindow:**水平或垂直排列小部件。**LabelFrame:**以復(fù)雜的結(jié)構(gòu)提供空間。**tkMessageBox:**在應(yīng)用程序中顯示消息框。現(xiàn)在,我們將簡(jiǎn)要介紹in out應(yīng)用程序中需要的一些小部件。請(qǐng)記住,這里我們將以**簡(jiǎn)單的示例演示該小部件。每個(gè)小部件中還有許多可用功能。在開(kāi)發(fā)游戲時(shí),我們會(huì)看到其中的一些。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
一些Tkinter小部件示例按鈕: 按鈕小部件用于在我們的應(yīng)用程序中顯示按鈕。通常,當(dāng)我們按下一個(gè)按鈕時(shí),將有一個(gè)與之關(guān)聯(lián)的命令。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries : from tkinter import * # Initialize tkinter : root = Tk() # Adding widgets : # Add button : btn = Button(root,text="PRESS ME",command=lambda:press()) # Place button in window : btn.grid(row=0,column=0) # Define the function : def press() lbl = Label(root,text="You Pressed The Button") lbl.grid(row=0,column=1) # Enter the main Loop : root.mainloop()復(fù)制代碼**標(biāo)簽:**標(biāo)簽小部件用于為我們應(yīng)用程序中的其他小部件提供單行標(biāo)題。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries : from tkinter import * # Initialize tkinter : root = Tk() # Adding widgets : # Add label : lbl = Label(root,text="This is label") # Place the button on window : lbl.grid(row=0,column=1) # Enter the main Loop : root.mainloop()復(fù)制代碼**畫(huà)布:**畫(huà)布小部件用于繪制各種形狀。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries : from tkinter import * # Initialize tkinter : root = Tk() # Adding widgets : # Add canvas : # Create canvas object : c = Canvas(root,bg="3389db",height=250,width-300) # Draw a straight line : line = c.create_line(0,0,50,50) # To fit the line in the window c.pack() # Enter the main loop root.mainloop()復(fù)制代碼**CheckButton:**我們使用checkbutton顯示可供用戶(hù)使用的多個(gè)選項(xiàng)。在這里,用戶(hù)可以選擇多個(gè)選項(xiàng)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries : from tkinter import * # Initialize tkinter : root = Tk() # Adding widgets : # Add checkbutton : # IntVar() hold integers # Default value is 0 # If checkbox is marked, this will change to 1 checkvar1 = IntVar() checkvar2 = IntVar() # Create checkbutton c1 = Checkbutton(root,text="BMW", variable=checkvar1) c2 = Checkbutton(root,text="Audi",variable=checkbar2) # To fit in the main window c1.grid(row=0,column=0) c2.grid(row=1,column=0) # Enter the main Loop root.mainloop()復(fù)制代碼Entry: Entry小部件用于接受用戶(hù)的單行輸入。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries from tkinter import * # Initialize tkinter root = Tk() # Adding widgets # Label lbl = Label(root,text="Enter your name:") lbl.grid(row=0,column=0) # Entry e = Entry(root) e.grid(row=0,column=1) # Enter the main Loop root.mainloop()復(fù)制代碼**框架:**用作容器小部件,以組織同一應(yīng)用程序中的其他小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries from tkinter import * # Initialize tkinter root = Tk() # Adding widgets frame = Frame(root) frame.pack() # Add button on Left A = Button(frame,text="A") A.pack(side = LEFT) # Add button on Right B = Button(frame,text="B") B.pack(side = RIGHT) # Enter the main Loop root.mainloop()復(fù)制代碼**列表框:**用于向用戶(hù)提供選項(xiàng)列表。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Import required libraries from tkinter import * # Initialize tkinter root = Tk() # Adding widgets # Create Listbox : Lb = Listbox(root) # Add items in list Lb.insert(1,"A") Lb.insert(2,"B") Lb.insert(3,"C") Lb.insert(4,"D") # Place listbox on window Lb.grid(row=0,column=0) # Enter the main Loop root.mainloop()復(fù)制代碼從頭開(kāi)始構(gòu)建數(shù)字猜謎游戲KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
分步演練當(dāng)用戶(hù)運(yùn)行程序時(shí),我們的代碼將生成一個(gè)介于0到9之間的隨機(jī)數(shù)。用戶(hù)將不知道隨機(jī)生成的數(shù)字?,F(xiàn)在,用戶(hù)必須猜測(cè)隨機(jī)生成的數(shù)字的值。用戶(hù)在輸入框中輸入值。之后,用戶(hù)將按下檢查按鈕。該按鈕將觸發(fā)功能。該功能將檢查用戶(hù)輸入的號(hào)碼是否與隨機(jī)生成的號(hào)碼匹配。如果猜測(cè)的數(shù)字正確,則程序?qū)@示正確的標(biāo)簽和實(shí)際數(shù)字(在這種情況下,該數(shù)字將與猜測(cè)的數(shù)字相同)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
現(xiàn)在,如果猜測(cè)的數(shù)字小于隨機(jī)生成的數(shù)字,則我們的程序?qū)@示TOO LOW標(biāo)簽,并且這還將清除輸入框,以便用戶(hù)可以輸入新的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
如果猜中的數(shù)字高于實(shí)際數(shù)字,則我們的程序?qū)@示TOO HIGH標(biāo)簽,并清除輸入框。這樣,用戶(hù)可以繼續(xù)猜測(cè)正確的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
如果標(biāo)簽顯示TOO HIGH,則用戶(hù)應(yīng)該輸入比他們猜想的數(shù)字低的數(shù)字。如果標(biāo)簽顯示TOO LOW,則用戶(hù)應(yīng)該輸入比他們第一次猜測(cè)的數(shù)字更大的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
我們的程序還將計(jì)算用戶(hù)猜測(cè)正確數(shù)字所需的嘗試次數(shù)。當(dāng)用戶(hù)**終做出正確的猜測(cè)時(shí),它將顯示總嘗試次數(shù)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
如果用戶(hù)想玩新游戲,則必須按下主關(guān)閉按鈕。如果用戶(hù)在我們的應(yīng)用程序中按下“ **關(guān)閉”**按鈕,則他們將完全退出游戲。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
只需簡(jiǎn)單的步驟即可:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
運(yùn)行應(yīng)用程序。輸入您的猜測(cè)。按下檢查按鈕。如果標(biāo)簽顯示不正確,請(qǐng)猜測(cè)一個(gè)新數(shù)字。如果標(biāo)簽顯示正確,則顯示嘗試次數(shù)。按下主關(guān)閉按鈕以新號(hào)碼重新開(kāi)始游戲。從我們的應(yīng)用程序中按下關(guān)閉按鈕以完全退出游戲。字猜謎游戲插圖(12)")我們將逐步向您展示如何使用Python tkinter構(gòu)建上述游戲。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
圖片素材在這里IT小站,此處下載數(shù)字猜謎游戲素材KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
步驟1:導(dǎo)入所需的庫(kù)KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# import required libraies : from tkinter import * # to add widgets import random # to generate a random number import tkinter.font as font # to change properties of font import simpleaudio as sa # to play sound files復(fù)制代碼步驟2:建立主Tkinter視窗KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
want_to_play = True while want_to_play==True: root = Tk() root.title("Guess The Number!") root.geometry('+100+0') root.configure(bg="#000000") root.resizable(width=False,height=False) root.iconphoto(True,PhotoImage(file="surprise.png"))復(fù)制代碼首先,我們創(chuàng)建一個(gè)名為的變量want_to_play,并將其值設(shè)置為T(mén)rue。當(dāng)變量設(shè)置為時(shí),我們的程序?qū)⑸梢粋€(gè)新窗口True。當(dāng)用戶(hù)按下主關(guān)閉按鈕時(shí),它將退出循環(huán),但是在這里,我們將變量設(shè)置為T(mén)rue,因此它將使用新生成的數(shù)字創(chuàng)建另一個(gè)窗口。root = Tk( ):用于初始化我們的tkinter模塊。root.title( ):我們使用它來(lái)設(shè)置應(yīng)用程序的標(biāo)題。root.geometry( ):我們使用它來(lái)指定我們的應(yīng)用程序窗口將在哪個(gè)位置打開(kāi)。root.configure( ):我們使用它來(lái)指定應(yīng)用程序的背景色。root.resizable( ):在這里我們使用它來(lái)防止用戶(hù)調(diào)整主窗口的大小。root.iconphoto( ):我們使用它來(lái)設(shè)置應(yīng)用程序窗口標(biāo)題欄中的圖標(biāo)。我們將第一個(gè)參數(shù)設(shè)置為T(mén)rue。步驟3:導(dǎo)入聲音文件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# to play sound files start = sa.WaveObject.from_wave_file("Start.wav") one = sa.WaveObject.from_wave_file("Win.wav") two = sa.WaveObjet.from_wave_file("Lose.wav") three = sa.WaveObject.from_wave_file("Draw.wav") start.play()復(fù)制代碼現(xiàn)在,我們將使用一些將在各種事件中播放的聲音文件。當(dāng)我們的程序啟動(dòng)時(shí),它將播放開(kāi)始文件。當(dāng)用戶(hù)的猜測(cè)正確,用戶(hù)的猜測(cè)錯(cuò)誤以及用戶(hù)關(guān)閉應(yīng)用程序時(shí),我們將分別播放其余三個(gè)文件。需要注意的一件事是它僅接受.wav文件。首先,我們需要將聲音文件加載到對(duì)象中。然后我們可以.play( )在需要時(shí)使用方法播放它。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
步驟4:為我們的應(yīng)用程序加載圖像KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
我們將在應(yīng)用程序中使用各種圖像。要首先使用這些圖像,我們需要加載這些圖像。在這里,我們將使用PhotoImage類(lèi)加載圖像。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Loading images Check = PhotoImage(file="Check_5.png") High = PhotoImage(file="High_5.png") Low = PhotoImage(file="Low_5.png") Correct = PhotoImage(file="Correct_5.png") Surprise = PhotoImage(file="Surprise.png") your_choice = PhotoImage(file="YOUR_GUESS.png") fingers = PhotoImage(file="fingers.png") close = PhotoImage(file="Close_5.png")復(fù)制代碼步驟5:產(chǎn)生隨機(jī)數(shù)KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在這里,我們將生成1–9之間的隨機(jī)數(shù)。我們將使用隨機(jī)模塊生成1–9之間的隨機(jī)整數(shù)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# generating random number number = random.randint(1,9)復(fù)制代碼步驟6:修改字體KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在這里,我們將使用字體模塊來(lái)更改應(yīng)用程序中的字體。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# using font module to modify fonts myFont = font.Font(family='Helvetica',weight='bold')復(fù)制代碼步驟7:添加小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在這里,我們添加了應(yīng)用程序的前兩個(gè)小部件。請(qǐng)注意,輸入框位于第2行,因?yàn)槲覀冊(cè)诘?行中添加了空格。在這里,我們將在標(biāo)簽中使用圖像文件。我們用于.grid( )指定特定小部件的位置。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Creating first label label = Label(root,image=your_choice) label.grid(row=0,column=1) # Creating the entry box e1 = Entry(root,bd=5,width=13,bg="9ca1db",justify=CENTER,font=myFont) e1.grid(row=2,column=1)復(fù)制代碼步驟8:添加其他小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在這里,我們將添加其他一些小部件,例如按鈕和標(biāo)簽。將有兩個(gè)按鈕,一個(gè)用于檢查值,另一個(gè)用于永久關(guān)閉窗口。第二個(gè)標(biāo)簽將顯示用戶(hù)猜測(cè)的值是正確還是高還是低。它將相應(yīng)地顯示標(biāo)簽。如果用戶(hù)的猜測(cè)正確,第三個(gè)標(biāo)簽將顯示正確的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
第四個(gè)標(biāo)簽顯示用戶(hù)猜測(cè)正確值所花費(fèi)的嘗試總數(shù)。在這里請(qǐng)注意,這兩個(gè)按鈕將觸發(fā)命令。在接下來(lái)的幾點(diǎn)中,我們將對(duì)此進(jìn)行研究。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Creating check button : b1 = Button(root,image=Check,command=lambda:show()) b1.grid(row=4,column=3) # Creating close button : b2 = Button(root,image=close,command=lambda:reset()) #Creaating second label : label2 = Label(root,image=fingers) label2.grid(row=6,column=1) #Creating third label : label3 = Label(root,image=Surprise) label3.grid(row=10,column=1) #Creating fourth label : label4= Label(root,text="ATTEMPTS : ",bd=5,width=13,bg="#34e0f2",justify=CENTER,font=myFont) label4.grid(row=12,column=1)復(fù)制代碼步驟9:顯示正確的圖像并將計(jì)數(shù)器設(shè)置為嘗試值KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
當(dāng)用戶(hù)的猜測(cè)正確時(shí),我們將在此處顯示正確的數(shù)字圖像。我們的數(shù)字存儲(chǔ)方式如下:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
1.png2.png3.png…100.png因此,我們的程序?qū)⒉捎脤?shí)際的數(shù)字,并在其中添加.png字符串并打開(kāi)該文件。我們還將設(shè)置計(jì)數(shù)器以計(jì)算嘗試值。它將存儲(chǔ)嘗試猜測(cè)正確數(shù)字所需的嘗試次數(shù)值。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# to display the correct image num = PhotoImage(file = str(number)+str(".png")) # Set the count to 0 count = 0復(fù)制代碼步驟10:當(dāng)我們按下檢查按鈕時(shí)將觸發(fā)的功能KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
在這里,每當(dāng)用戶(hù)按下檢查按鈕時(shí),嘗試次數(shù)的計(jì)數(shù)值將增加一。然后,我們將用戶(hù)輸入的值存儲(chǔ)在名為answer的變量中。然后,我們將檢查用戶(hù)是否尚未輸入任何值,并按下檢查按鈕,它將轉(zhuǎn)到reset()功能,應(yīng)用程序?qū)㈥P(guān)閉。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
現(xiàn)在,我們必須將用戶(hù)輸入的值轉(zhuǎn)換為整數(shù),以便將其與實(shí)際數(shù)字進(jìn)行比較。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
def show(): #Increase the count value as the user presses check button. global count count = count+1 #Get the value entered by user. answer = e1.get() #If the entry value is null the goto reset() function. if answer=="": reset() #Convert it to int for comparision. answer = int(e1.get()) if answer > number: #Play sound file. two.play() #Change the label to Too High. label2.configure(image=High) #Calls all pending idle tasks. root.update_idletasks() #Wait for 1 second. root.after(1000) #Clear the entry. e1.delete(0,"end") #Change the label to the original value. label2.configure(image=fingers) elif answer < number: #Play sound file. two.play() #Change the label to Too Low. label2.configure(image=Low) #Calls all pending idle tasks. root.update_idletasks() #Wait for 1 second. root.after(1000) #Clear the entry. e1.delete(0,"end") #Change the label to the original value. label2.configure(image=fingers) else: #Play sound file. one.play() #Show the CORRECT image. label2.configure(image=Correct) #Show the correct number. label3.configure(image=num) #Show the number of attempts. label4.configure(text="ATTEMPTS : "+str(count))復(fù)制代碼步驟11:“關(guān)閉”按鈕將觸發(fā)reset()功能KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
此函數(shù)會(huì)將want_to_play變量設(shè)置為,F(xiàn)alse以便我們的應(yīng)用程序關(guān)閉并且不會(huì)再次啟動(dòng)。然后它將關(guān)閉我們的應(yīng)用程序的主窗口。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# define reset() function def reset(): # Play sound file three.play() # Change the variable to false global want_to_play want_to_play = false # Close the tkinter window root.destroy()復(fù)制代碼步驟12:主循環(huán)KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
我們必須進(jìn)入主循環(huán)才能運(yùn)行程序。如果我們的程序沒(méi)有這一行,那么它將行不通。我們的程序?qū)⒈3衷谥餮h(huán)中,直到我們按下關(guān)閉按鈕。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
# Enter the mainLoop root.mainloop()復(fù)制代碼完整代碼#Import required libraries : from tkinter import * import random import tkinter.font as font import simpleaudio as sa want_to_play = True while want_to_play==True: root = Tk() root.title("Guess The Number!") root.geometry('+100+0') root.configure(bg="#000000") root.resizable(width=False,height=False) root.iconphoto(True,PhotoImage(file="surprise.png")) #To play sound files: start = sa.WaveObject.from_wave_file("Start.wav") one = sa.WaveObject.from_wave_file("Win.wav") two = sa.WaveObject.from_wave_file("Lose.wav") three = sa.WaveObject.from_wave_file("Draw.wav") start.play() #Loading images : Check = PhotoImage(file="Check_5.png") High = PhotoImage(file="High_5.png") Low = PhotoImage(file="Low_5.png") Correct = PhotoImage(file="Correct_5.png") Surprise= PhotoImage(file ="Surprise.png") your_choice = PhotoImage(file="YOUR_GUESS.png") fingers = PhotoImage(file = "Fingers.png") close = PhotoImage(file="Close_5.png") #To have space between rows. root.grid_rowconfigure(1, minsize=30) root.grid_rowconfigure(3, minsize=30) root.grid_rowconfigure(5, minsize=30) root.grid_rowconfigure(9, minsize=30) root.grid_rowconfigure(11, minsize=30) #Generating random number : number = random.randint(1,9) #Using font module to modify the fonts : myFont = font.Font(family='Helvetica',weight='bold') #Creating the first label : label = Label(root,image=your_choice) label.grid(row=0,column=1) #Creating the entry box : e1 = Entry(root,bd=5,width=13,bg="#9ca1db",justify=CENTER,font=myFont) e1.grid(row=2,column=1) #Creating check button : b1 = Button(root,image=Check,command=lambda:show()) b1.grid(row=4,column=3) #Creating close button : b2 = Button(root,image=close,command=lambda:reset()) b2.grid(row=4,column=0) #Creaating second label : label2 = Label(root,image=fingers) label2.grid(row=6,column=1) #Creating third label : label3 = Label(root,image=Surprise) label3.grid(row=10,column=1) #Creating fourth label : label4= Label(root,text="ATTEMPTS : ",bd=5,width=13,bg="#34e0f2",justify=CENTER,font=myFont) label4.grid(row=12,column=1) #To display the correct image : num = PhotoImage(file=str(number)+str(".png")) #Set the count to 0. #It stores the attempt value. count = 0 def show(): #Increase the count value as the user presses check button. global count count = count+1 #Get the value entered by user. answer = e1.get() #If the entry value is null the goto reset() function. if answer=="": reset() #Convert it to int for comparision. answer = int(e1.get()) if answer > number: #Play sound file. two.play() #Change the label to Too High. label2.configure(image=High) #Calls all pending idle tasks. root.update_idletasks() #Wait for 1 second. root.after(1000) #Clear the entry. e1.delete(0,"end") #Change the label to the original value. label2.configure(image=fingers) elif answer < number: #Play sound file. two.play() #Change the label to Too Low. label2.configure(image=Low) #Calls all pending idle tasks. root.update_idletasks() #Wait for 1 second. root.after(1000) #Clear the entry. e1.delete(0,"end") #Change the label to the original value. label2.configure(image=fingers) else: #Play sound file. one.play() #Show the CORRECT image. label2.configure(image=Correct) #Show the correct number. label3.configure(image=num) #Show the number of attempts. label4.configure(text="ATTEMPTS : "+str(count)) #Define reset() function : def reset(): #Play the sound file. three.play() #Change the variable to false. global want_to_play want_to_play = False #Close the tkinter window. root.destroy() #Enter the mainloop : root.mainloop()復(fù)制代碼相關(guān)免費(fèi)學(xué)習(xí)推薦:python視頻教程KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)
以上就是Python Tkinter教程 數(shù)字猜謎游戲的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注少兒編程網(wǎng)其它相關(guān)文章!KFT少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺(tái)

- 上一篇
如何配置python3的環(huán)境變量
簡(jiǎn)介Python的3.0版本,常被稱(chēng)為Python3000,或簡(jiǎn)稱(chēng)Py3k。相對(duì)于Python的早期版本,這是一個(gè)較大的升級(jí)。為了不帶入過(guò)多的累贅,Python3.0在設(shè)計(jì)的時(shí)候沒(méi)有考慮向下兼容。下載安裝python3程序Python官方的解釋器安裝包,可以 登錄Python官方網(wǎng)站 
- 下一篇
Python3目前最新版本是多少?
簡(jiǎn)介python3目前最新版本是python3.8.3:我們可以在python官方網(wǎng)站查看最新版本是多少:我們點(diǎn)擊進(jìn)入python3.8.3即可查看相關(guān)介紹:我們可以看到python3.8.3發(fā)布于2020年5月13日。Python3.8系列是Python編程語(yǔ)言的最新主要版本,它包含許多新特性和優(yōu)化。