您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Python+Tkinter實現經典井字棋小游戲”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用Python+Tkinter實現經典井字棋小游戲”吧!
首先來介紹一下GUI庫Tkinter
主要模塊:
tkinter Main Tkinter module.
tkinter.colorchooser
讓用戶選擇顏色的對話框。
tkinter.commondialog
本文其他模塊定義的對話框的基類。
tkinter.filedialog
允許用戶指定文件的通用對話框,用于打開或保存文件。
tkinter.font
幫助操作字體的工具。
tkinter.messagebox
訪問標準的 Tk 對話框。
tkinter.scrolledtext
內置縱向滾動條的文本組件。
tkinter.simpledialog
基礎對話框和一些便捷功能。
tkinter.ttk Themed widget set introduced in Tk 8.5, providing modern alternatives for many of the classic widgets in the main tkinter module.
一個小demo大家熟悉一下
from tkinter import * from tkinter import ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="Hello World!").grid(column=0, row=0) ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) root.mainloop()
我說的再好也不如去看看文檔,有什么不明白的可以下方留言。嘿嘿嘿傳送門
這部分其實我就是直接拿來用了沒怎么了解,我把經常用的給大家說說
class tkinter.messagebox.Message(master=None, **options) 創建一個默認信息消息框。 信息消息框 tkinter.messagebox.showinfo(title=None, message=None, **options) 警告消息框 tkinter.messagebox.showwarning(title=None, message=None, **options) tkinter.messagebox.showerror(title=None, message=None, **options) 疑問消息框 tkinter.messagebox.askquestion(title=None, message=None, **options) tkinter.messagebox.askokcancel(title=None, message=None, **options) tkinter.messagebox.askretrycancel(title=None, message=None, **options) tkinter.messagebox.askyesno(title=None, message=None, **options) tkinter.messagebox.askyesnocancel(title=None, message=None, **options)
from tkinter import * import tkinter.messagebox as msg root = Tk() root.title('TIC-TAC-TOE---Project Gurukul') # labels Label(root, text="player1 : X", font="times 15").grid(row=0, column=1) Label(root, text="player2 : O", font="times 15").grid(row=0, column=2) digits = [1, 2, 3, 4, 5, 6, 7, 8, 9] # for player1 sign = X and for player2 sign= Y mark = '' # counting the no. of click count = 0 panels = ["panel"] * 10 def win(panels, sign): return ((panels[1] == panels[2] == panels[3] == sign) or (panels[1] == panels[4] == panels[7] == sign) or (panels[1] == panels[5] == panels[9] == sign) or (panels[2] == panels[5] == panels[8] == sign) or (panels[3] == panels[6] == panels[9] == sign) or (panels[3] == panels[5] == panels[7] == sign) or (panels[4] == panels[5] == panels[6] == sign) or (panels[7] == panels[8] == panels[9] == sign)) def checker(digit): # 檢查按鍵 if digit == 1 and digit in digits: digits.remove(digit) ##player1 will play if the value of count is even and for odd player2 will play if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button1.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 2 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button2.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 3 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button3.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 4 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button4.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 5 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button5.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 6 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button6.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 7 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button7.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 8 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button8.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 9 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button9.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() ###if count is greater then 8 then the match has been tied if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False): msg.showinfo("Result", "Match Tied") root.destroy() ####定義每個格子 button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1)) button1.grid(row=1, column=1) button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2)) button2.grid(row=1, column=2) button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3)) button3.grid(row=1, column=3) button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4)) button4.grid(row=2, column=1) button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5)) button5.grid(row=2, column=2) button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6)) button6.grid(row=2, column=3) button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7)) button7.grid(row=3, column=1) button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8)) button8.grid(row=3, column=2) button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9)) button9.grid(row=3, column=3)
感謝各位的閱讀,以上就是“怎么用Python+Tkinter實現經典井字棋小游戲”的內容了,經過本文的學習后,相信大家對怎么用Python+Tkinter實現經典井字棋小游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。