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() 

 

相關文章
相關標籤/搜索