Python开发转盘小游戏

Python开发转盘小游戏

一 原理分析

  1. Python开发一个图形界面
  2. 有12个选项和2个功能键
  3. 确定每个按钮的位置
    每个按钮的间隔相同
  4. 点击开始时转动,当前选项的背景颜色为红色,其他为白色

二 知识点与难点

2.1 使用的package

  1. tkinter
    Python内置,实现GUI的主要package
  2. threading
    实现多线程实现循环
  3. time
    用来延时

2.2 注意点

  1. 判断类型时使用isinstance函数,不要用type,因为type不区分子类
  2. 判断变量是否为TRUE,FALSE,None时要用is不要用==
  3. 循环时记得处理自增时超过下标时的归零
  4. tkinter下面的按钮属性可以通过键值赋值改变属性
  5. 全局变量

三 代码

  1. import tkinter 
  2. import threading 
  3. import time 
  4. root = tkinter.Tk() 
  5. # 设置窗口名字
  6. root.title('香港男神'
  7. # 设置窗口大小
  8. root.minsize(300, 300
  9. # 按钮
  10. btn1 = tkinter.Button(root, text='成龙', bg='red'
  11. btn1.place(x=20, y=20, width=50, height=50
  12. btn2 = tkinter.Button(root, text='张国荣', bg='white'
  13. btn2.place(x=90, y=20, width=50, height=50
  14. btn3 = tkinter.Button(root, text='郭富城', bg='white'
  15. btn3.place(x=160, y=20, width=50, height=50
  16. btn4 = tkinter.Button(root, text='黎明', bg='white'
  17. btn4.place(x=230, y=20, width=50, height=50
  18. btn5 = tkinter.Button(root, text='刘德华', bg='white'
  19. btn5.place(x=230, y=90, width=50, height=50
  20. btn6 = tkinter.Button(root, text='梁朝伟', bg='white'
  21. btn6.place(x=230, y=160, width=50, height=50
  22. btn7 = tkinter.Button(root, text='张学友', bg='white'
  23. btn7.place(x=230, y=230, width=50, height=50
  24. btn8 = tkinter.Button(root, text='周润发', bg='white'
  25. btn8.place(x=160, y=230, width=50, height=50
  26. btn9 = tkinter.Button(root, text='周星驰', bg='white'
  27. btn9.place(x=90, y=230, width=50, height=50
  28. btn10 = tkinter.Button(root, text='谢霆锋', bg='white'
  29. btn10.place(x=20, y=230, width=50, height=50
  30. btn11 = tkinter.Button(root, text='张家辉', bg='white'
  31. btn11.place(x=20, y=160, width=50, height=50
  32. btn12 = tkinter.Button(root, text='古天乐', bg='white'
  33. btn12.place(x=20, y=90, width=50, height=50
  34. # 按钮列表
  35. herolist = [btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12] 
  36. # 开启的标志
  37. isloop = False
  38. # 停止的标志
  39. isstop = False
  40. # 停止时的ID
  41. stopid = None
  42. # 定义一个函数,实现循环功能
  43. def round():
  44. # 设为全局变量,在此处做的改动可应用在其他函数中
  45. global isloop 
  46. global stopid 
  47. # 设置ID开始值
  48. i = 1
  49. # 已经在循环中
  50. if isloop is True
  51. return
  52. if isinstance(stopid, int): 
  53. i = stopid 
  54. # 开始循环
  55. while True
  56. # 睡眠
  57. time.sleep(0.03
  58. # 设置每个按钮的背景色
  59. # 可以通过键值得方式设置按钮属性
  60. for e_btn in herolist: 
  61. e_btn['bg'] = 'white'
  62. # 当前按钮背景色设置为红色
  63. herolist[i]['bg'] = 'red'
  64. i += 1
  65. print('当前i为', i) 
  66. # 当i的值大于选项个数时,归零
  67. if i >= 12
  68. i = 0
  69. # 当停止按钮被激活时,停止循环
  70. if isstop is True
  71. isloop = False
  72. stopid = i 
  73. break
  74. # 定义停止函数,只是将停止标志设置为True
  75. def my_stop():
  76. global isstop 
  77. # 已经是停止状态时,不变
  78. if isstop is True
  79. return
  80. isstop = True
  81. # 定义开始函数
  82. def newtask():
  83. global isloop 
  84. global isstop 
  85. isstop = False
  86. # 使用线程
  87. t = threading.Thread(target=round) 
  88. t.start() 
  89. # 开启循环标志
  90. isloop = True
  91. # 设置开始按钮
  92. btn_start = tkinter.Button(root, text='开始', command=newtask) 
  93. btn_start.place(x=90, y=125, width=50, height=50
  94. # 设置停止按钮
  95. btn_stop = tkinter.Button(root, text='停止', command=my_stop) 
  96. btn_stop.place(x=160, y=125, width=50, height=50
  97. root.mainloop() 

 

 

posted @ 2018-07-23 21:26  banshaohuan  阅读(1083)  评论(0编辑  收藏  举报