井字棋,叫上同桌来一把?
【摘要】
演示
介绍
首先来介绍一下GUI库Tkinter
主要模块:
tkinter
Main Tkinter module.
12
tkinter.colorchooser
让用户选择颜色的对话...
演示
介绍
首先来介绍一下GUI库Tkinter
主要模块:
tkinter
Main Tkinter module.
- 1
- 2
tkinter.colorchooser
让用户选择颜色的对话框。
- 1
- 2
tkinter.commondialog
本文其他模块定义的对话框的基类。
- 1
- 2
tkinter.filedialog
允许用户指定文件的通用对话框,用于打开或保存文件。
- 1
- 2
tkinter.font
帮助操作字体的工具。
- 1
- 2
tkinter.messagebox
访问标准的 Tk 对话框。
- 1
- 2
tkinter.scrolledtext
内置纵向滚动条的文本组件。
- 1
- 2
tkinter.simpledialog
基础对话框和一些便捷功能。
- 1
- 2
tkinter.ttk
Themed widget set introduced in Tk 8.5, providing modern alternatives for many of the classic widgets in the main tkinter module.
- 1
- 2
hello word!
一个小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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
官方文档
我说的再好也不如去看看文档,有什么不明白的可以下方留言。嘿嘿嘿
传送门
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
源码
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
本文来自:趣学python+python办公专栏欢迎大家一起学习。
链接:传送门
点击直接资料领取
有问题欢迎私信或留言
这里有python,Java学习资料还有有有趣好玩的编程项目,更有难寻的各种资源。反正看看也不亏。
文章来源: blog.csdn.net,作者:肥学,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jiahuiandxuehui/article/details/123470885
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)