演示

介绍

首先来介绍一下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()

官方文档

我说的再好也不如去看看文档,有什么不明白的可以下方留言。嘿嘿嘿传送门

tkinter.messagebox

这部分其实我就是直接拿来用了没怎么了解,我把经常用的给大家说说

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井字棋的资料请关注www.887551.com其它相关文章!