今天我们用python+tkinter
安装带界面的井字棋,效果如图所示。
tkinter
是 python
的标准 gui 库。python
使用 tkinter 可以快速的创建 gui 应用程序。由于 tkinter
是内置到 python 的安装包中、只要安装好 python 之后就能 import tkinter
库、而且 idle 也是用 tkinter
编写而成、对于简单的图形界面 tkinter
还是能应付自如。
1
|
pip install tkinter |
1、首先安装tkinter
1
2
3
4
5
6
7
|
root = tk() root.title( '井字棋' ) digits = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] mark = '' “ count = 0 panels = [ "panel" ] * 10 |
2、初始化窗口
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
|
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 ) 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 ) root.mainloop() |
3、定义按钮
很明显上面的井字棋就是9个按钮。这里定义9个按钮。
1
2
3
4
5
6
7
8
9
|
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)) |
4、检查获胜的条件
检查获胜的条件,上面其中一种情况都是获胜的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
def checker(digit): global count, mark, digits if digit = = 1 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mar 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() |
最后这里是点击下棋,然后用win
函数判断是否结束比赛赢得棋局。
到此这篇关于python
实现带界面的井字棋的小游戏的文章就介绍到这了,更多相关python
实现带界面的井字棋内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.cn/post/7010574460176564238