python -猜字小游戏

Posted MUXI_King

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python -猜字小游戏相关的知识,希望对你有一定的参考价值。

代码运行效果如下:

注意:

1.必须要在python3环境想使用

2.QQ:3084276329(一起交流学习)

3.还请大家评论

Guess the word game代码如下:

 

  1 #! /usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # Guess the word game
  4 # 博客:https://www.cnblogs.com/muxii
  5 # 那个木兮啊
  6 import tkinter
  7 import threading
  8 import time
  9 from tkinter import *
 10 from tkinter import  messagebox
 11 from tkinter import  messagebox
 12 
 13 root = tkinter.Tk()
 14 root.title(\'那个木兮啊-Guess the word\')
 15 root.minsize(300,330)
 16 
 17 label =(root)
 18 label = Label(root,text = \'博客:https://www.cnblogs.com/muxii/\\nQQ:3084276329\\n—-————那个木兮啊——-———\',font = (\'微软雅黑\',11),fg = \'red\')
 19 label.grid(row =0 ,column =0)
 20 btn1 = tkinter.Button(root,text =\'1\',width = 5,height =2,bg=\'blue\')
 21 btn1.place(x=20,y=70)
 22 btn2 = tkinter.Button(root,text = \'2\',width = 5,height =2,bg =\'white\')
 23 btn2.place(x=90,y=70)
 24 btn3 = tkinter.Button(root,text = \'3\',width = 5,height =2,bg =\'white\')
 25 btn3.place(x=160,y=70)
 26 btn4 = tkinter.Button(root,text = \'3\',width = 5,height =2,bg =\'white\')
 27 btn4.place(x=230,y=140)
 28 btn5 = tkinter.Button(root,text = \'5\',width = 5,height =2,bg =\'white\')
 29 btn5.place(x=230,y=70)
 30 btn6 = tkinter.Button(root,text = \'6\',width = 5,height =2,bg =\'white\')
 31 btn6.place(x=230,y=210)
 32 btn7 = tkinter.Button(root,text = \'7\',width = 5,height =2,bg =\'white\')
 33 btn7.place(x=230,y=280 )
 34 btn8 = tkinter.Button(root,text = \'8\',width = 5,height =2,bg =\'white\')
 35 btn8.place(x=160,y=280)
 36 btn9 = tkinter.Button(root,text = \'9\',width = 5,height =2,bg =\'white\')
 37 btn9.place(x=90,y=280 )
 38 btn10 = tkinter.Button(root,text = \'10\',width = 5,height =2,bg =\'white\')
 39 btn10.place(x=20,y=280)
 40 btn11 = tkinter.Button(root,text = \'11\',width = 5,height =2,bg =\'white\')
 41 btn11.place(x=20,y=210)
 42 btn12 = tkinter.Button(root,text = \'12\',width = 5,height =2,bg =\'white\')
 43 btn12.place(x=20,y=140)
 44 herolists = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn11]
 45 
 46 isloop    = False
 47 
 48 stopsing  = False
 49 #定义停止id
 50 stopid    = None
 51 
 52 #定义函数,循环选项 设置背景颜色,选中的为红色,
 53 def round():
 54     global isloop
 55     global stopid
 56     if isloop ==True:
 57         return
 58     i = 1
 59     #判断一个对象是否已经类型 type
 60     #考虑继承关系
 61     if isinstance(stopid,int):
 62         i = stopid
 63 
 64     while True:
 65         #
 66         time.sleep(0.05)#延时
 67         for x in herolists:
 68             x[\'bg\'] = \'white\'
 69 
 70         herolists[i][\'bg\'] =\'blue\'
 71 
 72         i += 1
 73 
 74 
 75         if i >= len(herolists):
 76             i = 0
 77 
 78         if  stopsing == True:
 79             isloop =False
 80             stopid = i
 81             break
 82 
 83  #停止
 84 def stop1():
 85 
 86     global stopsing
 87 
 88     if stopsing == True:
 89         return
 90     stopsing = True
 91 
 92 #开始
 93 def newtask():
 94     global  isloop
 95     global  stopsing
 96     stopsing = False
 97 #建立线程
 98     t = threading.Thread(target = round )
 99     t.start()
100     isloop = True
101 
102 btn_start = tkinter.Button(root,text = \'开始\',bg = \'red\',command = newtask)
103 btn_start.place(x=90,y=175)
104 #
105 btn_stop = tkinter.Button(root,text = \'结束\',bg = \'yellow\',command = stop1)
106 btn_stop.place(x=160,y=175 )
107 
108 root.mainloop()

 

2018.8.3

以上是关于python -猜字小游戏的主要内容,如果未能解决你的问题,请参考以下文章

Python开发

Python 介绍

Python学习之认识python

python初识

Python 线程池模块,Python 多线程

如何切换python版本