<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
首先來介紹一下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井字棋的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45