国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內(nèi)容

Python Tkinter教程 數(shù)字猜謎游戲

極客小將2020-12-31-
簡介python視頻教程欄目以數(shù)字猜謎游戲介紹Tkinter。Tkinter是Python的TkGUI(圖形用戶界面)工具包和事實上的標準GUI的標準接口。GUI使您可以使用大多數(shù)操作系統(tǒng)使用的可視項(例如窗口,圖標和菜單)與計算機進行交互。這個功能強大的工具可用于構建各種項目,并且使可視化代碼更加容易
python視頻教程欄目以數(shù)字猜謎游戲介紹Tkinter。

KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

Tkinter是Python的Tk GUI(圖形用戶界面)工具包和事實上的標準GUI 的標準接口。GUI使您可以使用大多數(shù)操作系統(tǒng)使用的可視項(例如窗口,圖標和菜單)與計算機進行交互。這個功能強大的工具可用于構建各種項目,并且使可視化代碼更加容易。
KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在本文中,我們將了解Tkinter的基礎知識以及可在Python應用程序中使用的不同類型的小部件。在本文的后面,我們將使用Tkinter小部件開發(fā)一個很酷的數(shù)字猜測游戲。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

今天,我們將介紹:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

Tkinter的基礎Tkinter的小部件與示例從頭開始構建數(shù)字猜謎游戲Tkinter的基礎

在構建游戲之前,我們需要了解Tkinter的一些基礎知識。Tkinter軟件包是Tk GUI工具包的標準python接口。我們通常使用Tkinter包在應用程序中插入不同的GUI小部件,以使其更加用戶友好。如果您在Linux,Windows或Mac上使用Python,則設備上已經(jīng)安裝了Python Tkinter。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

我們?nèi)绾伍_發(fā)GUI應用程序?

創(chuàng)建GUI應用程序的基本過程如下:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

Import the Tkinter ModuleCreate Main WindowAdd WidgetsEnter Main LoopKFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

使用Python開發(fā)GUI應用程序涉及的步驟:導入tkinter模塊。為我們的GUI應用程序創(chuàng)建主窗口。現(xiàn)在,為我們的應用程序添加任意數(shù)量的小部件。進入主事件循環(huán)以執(zhí)行我們的主要功能。

現(xiàn)在讓我們看看如何創(chuàng)建一個簡單的tkinter窗口:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

首先,我們將導入tkinter模塊。它包含構建應用程序所需的所有功能,類和其他內(nèi)容。現(xiàn)在,當我們導入模塊時,我們需要初始化tkinter。為此,我們創(chuàng)建Tk( )根窗口小部件。現(xiàn)在,這將創(chuàng)建我們的主GUI窗口,我們將在其中添加小部件。此時,我們的主窗口只有標題欄。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

我們應該只為我們的應用程序創(chuàng)建一個窗口,并且必須在添加任何其他小部件之前創(chuàng)建該窗口。之后,我們使用root.mainloop( )。除非輸入,否則不會顯示我們剛剛創(chuàng)建的主窗口mainloop。當我們按下關閉按鈕時,我們的程序?qū)⑼顺鲋餮h(huán)。在按下關閉按鈕之前,我們的應用程序?qū)⒁恢边\行。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

用于創(chuàng)建簡單的tkinter窗口的代碼:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

#import required libraries from tkinter import * # initialize tkinter : root = Tk() # enter the main Loop : root.mainloop()復制代碼Tkinter的小部件與示例**按鈕:**顯示按鈕。**畫布:**繪制形狀。**復選框:**將多個選項顯示為復選框。**輸入:**接受用戶的單行輸入。**框架:**組織其他小部件。**標簽:**為其他小部件添加標題。**列表框:**向用戶提供選項列表。菜單**按鈕:**在我們的應用程序中顯示菜單。**菜單:**向用戶提供各種命令。**消息:**顯示多行文本字段。**單選按鈕:**將選項數(shù)量顯示為單選按鈕。**比例尺:**提供滑塊。**滾動條:**添加滾動功能。**文字:**以多行顯示文字。**頂層:**提供單獨的窗口容器。**Spinbox:**從固定輸入值中選擇。**PanedWindow:**水平或垂直排列小部件。**LabelFrame:**以復雜的結(jié)構提供空間。**tkMessageBox:**在應用程序中顯示消息框。

現(xiàn)在,我們將簡要介紹in out應用程序中需要的一些小部件。請記住,這里我們將以**簡單的示例演示該小部件。每個小部件中還有許多可用功能。在開發(fā)游戲時,我們會看到其中的一些。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

一些Tkinter小部件示例

按鈕: 按鈕小部件用于在我們的應用程序中顯示按鈕。通常,當我們按下一個按鈕時,將有一個與之關聯(lián)的命令。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

**標簽:**標簽小部件用于為我們應用程序中的其他小部件提供單行標題。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

**畫布:**畫布小部件用于繪制各種形狀。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

**CheckButton:**我們使用checkbutton顯示可供用戶使用的多個選項。在這里,用戶可以選擇多個選項。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

Entry: Entry小部件用于接受用戶的單行輸入。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

**框架:**用作容器小部件,以組織同一應用程序中的其他小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

**列表框:**用于向用戶提供選項列表。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

從頭開始構建數(shù)字猜謎游戲KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

分步演練

當用戶運行程序時,我們的代碼將生成一個介于0到9之間的隨機數(shù)。用戶將不知道隨機生成的數(shù)字。現(xiàn)在,用戶必須猜測隨機生成的數(shù)字的值。用戶在輸入框中輸入值。之后,用戶將按下檢查按鈕。該按鈕將觸發(fā)功能。該功能將檢查用戶輸入的號碼是否與隨機生成的號碼匹配。如果猜測的數(shù)字正確,則程序?qū)@示正確的標簽和實際數(shù)字(在這種情況下,該數(shù)字將與猜測的數(shù)字相同)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

現(xiàn)在,如果猜測的數(shù)字小于隨機生成的數(shù)字,則我們的程序?qū)@示TOO LOW標簽,并且這還將清除輸入框,以便用戶可以輸入新的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

如果猜中的數(shù)字高于實際數(shù)字,則我們的程序?qū)@示TOO HIGH標簽,并清除輸入框。這樣,用戶可以繼續(xù)猜測正確的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

如果標簽顯示TOO HIGH,則用戶應該輸入比他們猜想的數(shù)字低的數(shù)字。如果標簽顯示TOO LOW,則用戶應該輸入比他們第一次猜測的數(shù)字更大的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

我們的程序還將計算用戶猜測正確數(shù)字所需的嘗試次數(shù)。當用戶**終做出正確的猜測時,它將顯示總嘗試次數(shù)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

如果用戶想玩新游戲,則必須按下主關閉按鈕。如果用戶在我們的應用程序中按下“ **關閉”**按鈕,則他們將完全退出游戲。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

只需簡單的步驟即可:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

運行應用程序。輸入您的猜測。按下檢查按鈕。如果標簽顯示不正確,請猜測一個新數(shù)字。如果標簽顯示正確,則顯示嘗試次數(shù)。按下主關閉按鈕以新號碼重新開始游戲。從我們的應用程序中按下關閉按鈕以完全退出游戲。

![Python Tkinter教程系列02:數(shù)字猜謎游戲插圖(12)](https://img.php.cn/upload/article/000/000/052/a829e9a273177c9e7f7b0cf1d39a0ef6-0.jpg "Python Tkinter教程系列02:數(shù)字猜謎游戲插圖(12)")我們將逐步向您展示如何使用Python tkinter構建上述游戲。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

圖片素材在這里IT小站,此處下載數(shù)字猜謎游戲素材KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

步驟1:導入所需的庫KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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復制代碼

步驟2:建立主Tkinter視窗KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

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"))復制代碼首先,我們創(chuàng)建一個名為的變量want_to_play,并將其值設置為True。當變量設置為時,我們的程序?qū)⑸梢粋€新窗口True。當用戶按下主關閉按鈕時,它將退出循環(huán),但是在這里,我們將變量設置為True,因此它將使用新生成的數(shù)字創(chuàng)建另一個窗口。root = Tk( ):用于初始化我們的tkinter模塊。root.title( ):我們使用它來設置應用程序的標題。root.geometry( ):我們使用它來指定我們的應用程序窗口將在哪個位置打開。root.configure( ):我們使用它來指定應用程序的背景色。root.resizable( ):在這里我們使用它來防止用戶調(diào)整主窗口的大小。root.iconphoto( ):我們使用它來設置應用程序窗口標題欄中的圖標。我們將第一個參數(shù)設置為True。

步驟3:導入聲音文件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

現(xiàn)在,我們將使用一些將在各種事件中播放的聲音文件。當我們的程序啟動時,它將播放開始文件。當用戶的猜測正確,用戶的猜測錯誤以及用戶關閉應用程序時,我們將分別播放其余三個文件。需要注意的一件事是它僅接受.wav文件。首先,我們需要將聲音文件加載到對象中。然后我們可以.play( )在需要時使用方法播放它。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

步驟4:為我們的應用程序加載圖像KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

我們將在應用程序中使用各種圖像。要首先使用這些圖像,我們需要加載這些圖像。在這里,我們將使用PhotoImage類加載圖像。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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")復制代碼

步驟5:產(chǎn)生隨機數(shù)KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在這里,我們將生成1–9之間的隨機數(shù)。我們將使用隨機模塊生成1–9之間的隨機整數(shù)。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# generating random number number = random.randint(1,9)復制代碼

步驟6:修改字體KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在這里,我們將使用字體模塊來更改應用程序中的字體。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# using font module to modify fonts myFont = font.Font(family='Helvetica',weight='bold')復制代碼

步驟7:添加小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在這里,我們添加了應用程序的前兩個小部件。請注意,輸入框位于第2行,因為我們在第1行中添加了空格。在這里,我們將在標簽中使用圖像文件。我們用于.grid( )指定特定小部件的位置。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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)復制代碼

步驟8:添加其他小部件KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在這里,我們將添加其他一些小部件,例如按鈕和標簽。將有兩個按鈕,一個用于檢查值,另一個用于永久關閉窗口。第二個標簽將顯示用戶猜測的值是正確還是高還是低。它將相應地顯示標簽。如果用戶的猜測正確,第三個標簽將顯示正確的數(shù)字。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

第四個標簽顯示用戶猜測正確值所花費的嘗試總數(shù)。在這里請注意,這兩個按鈕將觸發(fā)命令。在接下來的幾點中,我們將對此進行研究。 KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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)復制代碼

步驟9:顯示正確的圖像并將計數(shù)器設置為嘗試值KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

當用戶的猜測正確時,我們將在此處顯示正確的數(shù)字圖像。我們的數(shù)字存儲方式如下:KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

1.png2.png3.png…100.png

因此,我們的程序?qū)⒉捎脤嶋H的數(shù)字,并在其中添加.png字符串并打開該文件。我們還將設置計數(shù)器以計算嘗試值。它將存儲嘗試猜測正確數(shù)字所需的嘗試次數(shù)值。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# to display the correct image num = PhotoImage(file = str(number)+str(".png")) # Set the count to 0 count = 0復制代碼

步驟10:當我們按下檢查按鈕時將觸發(fā)的功能KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

在這里,每當用戶按下檢查按鈕時,嘗試次數(shù)的計數(shù)值將增加一。然后,我們將用戶輸入的值存儲在名為answer的變量中。然后,我們將檢查用戶是否尚未輸入任何值,并按下檢查按鈕,它將轉(zhuǎn)到reset()功能,應用程序?qū)㈥P閉。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

現(xiàn)在,我們必須將用戶輸入的值轉(zhuǎn)換為整數(shù),以便將其與實際數(shù)字進行比較。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

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))復制代碼

步驟11:“關閉”按鈕將觸發(fā)reset()功能KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

此函數(shù)會將want_to_play變量設置為,F(xiàn)alse以便我們的應用程序關閉并且不會再次啟動。然后它將關閉我們的應用程序的主窗口。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# 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()復制代碼

步驟12:主循環(huán)KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

我們必須進入主循環(huán)才能運行程序。如果我們的程序沒有這一行,那么它將行不通。我們的程序?qū)⒈3衷谥餮h(huán)中,直到我們按下關閉按鈕。KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

# Enter the mainLoop root.mainloop()復制代碼完整代碼#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()復制代碼

相關免費學習推薦:python視頻教程KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

以上就是Python Tkinter教程 數(shù)字猜謎游戲的詳細內(nèi)容,更多請關注少兒編程網(wǎng)其它相關文章!KFT少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 玉环县| 沂源县| 弋阳县| 曲水县| 沭阳县| 三明市| 马边| 肃宁县| 海伦市| 洪洞县| 康乐县| 曲水县| 即墨市| 辉县市| 永德县| 连云港市| 南漳县| 大关县| 阿拉善左旗| 湘潭市| 潜江市| 湖北省| 德令哈市| 株洲市| 远安县| 兴化市| 绿春县| 栖霞市| 兴义市| 进贤县| 邹城市| 获嘉县| 神木县| 清河县| 津市市| 东山县| 府谷县| 云南省| 丁青县| 潮安县| 马山县|