注:本文僅用於學習交流分享,[若有不妥之處,請指正,感謝]
關鍵詞:【python】【tkinter】
最後面有 程序 分享
用到的工具有:
- Python 3.6
實現的小功能有:
實現簡易幸運轉盤式抽獎界面
一、相關函數方法介紹
Tkinter 是 Python 的標準 GUI 庫。Python 使用 Tkinter 可以快速的創建 GUI 應用程序。
Tkinter 是 Python 的標準 GUI 庫。Python 使用 Tkinter 可以快速的創建 GUI 應用程序。由於 Tkinter 是內置到 python 的安裝包中、只要安裝好 Python 之後就能 import Tkinter 庫。
1.創建窗口【Tk】
- ①導入 Tkinter 模塊
- ②創建控件
- ③指定這個控件的 master, 即這個控件屬於哪一個
#示例
from tkinter as tk # 導入tk庫
root = Tk() #初始化Tk() 建立一個窗口
root.mainloop() #進入消息循環,時刻刷新窗口
2.創建標籤【Label】
用法:
Label(根對象, [屬性列表])
屬性 | 可選項 & 描述 |
---|---|
text | 標籤文本 |
bg | 背景顏色 |
font | 字體(顏色, 大小) |
width | 控件寬度 |
height | 控件高度 |
#示例
#創建標籤label1
label1 = tk.Label(root, text='謝謝惠顧', bg='yellow', font=('Arial', 50))
#設置標籤位置及大小
label1.place(x=0, y=600, width=390, height=250)
3.創建按鈕【Button】
用法:
Button (根對象, [屬性列表])
屬性 | 可選項 & 描述 |
---|---|
activebackground | 當鼠標放上去時,按鈕的背景色 |
activeforeground | 當鼠標放上去時,按鈕的前景色 |
bg | 按鈕的背景色 |
font | 文本字體(顏色, 大小) |
justify | 顯示多行文本的時候,設置不同行之間的對齊方式,可選項包括LEFT, RIGHT, CENTER |
height | 按鈕的高度 |
image | 按鈕上要顯示的圖片 |
padx | 按鈕在x軸方向上的內邊距(padding),是指按鈕的內容與按鈕邊緣的距離 |
pady | 按鈕在y軸方向上的內邊距(padding) |
relief | 邊框樣式,設置控件3D效果,可選的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認爲 FLAT。 |
width | 按鈕的寬度,如未設置此項,其大小以適應按鈕的內容(文本或圖片的大小) |
height | 控件高度 |
#示例
# 設置啓動按鍵 背景文本爲“RUN” 底色爲“天藍” 字體“Arial” 字體大小“50” 回調函數command 爲【滾動函數】
btn1 = tk.Button(root, text='RUN', bg='lightSkyBlue', font=('Arial', 50), command=round)
#設置按鍵座標及大小
btn1.place(x=800, y=850, width=200, height=200)
二、簡易滾動抽獎界面代碼
import time
import threading
from PIL import Image
import tkinter as tk # 導入 tk庫 模塊
import random # 導入 隨機庫 模塊
root = tk.Tk() #初始化Tk() 建立一個窗口
root.title('集大電協簡易抽獎') # 設置標題
root.minsize(1000, 700)
photo = tk.PhotoImage(file="ETA.png") # file:圖片路徑
imgLabel = tk.Label(root, image=photo) # 把圖片整合到標籤類中
imgLabel.pack(side=tk.RIGHT) # 右對齊
label1 = tk.Label(root, text='謝謝惠顧', bg='yellow', font=('Arial', 50))
label1.place(x=0, y=600, width=390, height=250)
label2 = tk.Label(root, text='簡易四軸', bg='yellow', font=('Arial', 50))
label2.place(x=0, y=10, width=390, height=250)
label3 = tk.Label(root, text='謝謝惠顧', bg='yellow', font=('Arial', 50))
label3.place(x=390, y=10, width=390, height=250)
label4 = tk.Label(root, text='mini光立方', bg='yellow', font=('Arial', 50))
label4.place(x=780, y=10, width=390, height=250)
label5 = tk.Label(root, text='再來一次', bg='yellow', font=('Arial', 50))
label5.place(x=1170, y=10, width=390, height=250)
label6 = tk.Label(root, text='謝謝惠顧', bg='yellow', font=('Arial', 50))
label6.place(x=1560, y=10, width=390, height=250)
label7 = tk.Label(root, text='幸運轉盤PCB', bg='yellow', font=('Arial', 50))
label7.place(x=1560, y=600, width=390, height=250)
label8 = tk.Label(root, text='謝謝惠顧', bg='yellow', font=('Arial', 50))
label8.place(x=1170, y=600, width=390, height=250)
label9 = tk.Label(root, text='51核心板', bg='yellow', font=('Arial', 50))
label9.place(x=780, y=600, width=390, height=250)
label10 = tk.Label(root, text='再來一次', bg='yellow', font=('Arial', 50))
label10.place(x=390, y=600, width=390, height=250)
label11 = tk.Label(root, text='最終解釋權歸【集美大學學·生電子技術協會】所有', bg='white', font=('Arial', 20))
label11.place(x=1250, y=900, width=700, height=100)
# 將所有抽獎選項添加到列表
things = [label1, label2, label3, label4, label5, label6, label7, label8, label9, label10]
# 獲取列表的最大索引值
maxvalue = len(things) - 1
# 設置起始值爲隨機整數
starts = random.randint(0, 6)
# 是否停止標誌
notround = False
# 定義滾動函數
def round():
t = threading.Thread(target=startup) #啓動start
t.start()
# 定義開始函數
def startup():
global starts
global notround
while True:
# 檢測停止按鈕是否被按下
if notround == True:
notround = False
return starts
# 程序延時
time.sleep(0.017)
# 在所有抽獎選項中循環滾動
for i in things:
i['bg'] = 'lightSkyBlue' #開始時 底色變成天藍
things[starts]['bg'] = 'red' #滾動框爲 紅色
starts += 1
if starts > maxvalue:
starts = 0
# 定義停止函數
def stops():
global notround # notround 爲全局變量
global starts
notround = True #停止標誌位
if starts == 1: # 如果抽中“簡易四軸”就跳轉爲“謝謝惠顧”【奸商^_^】
starts = 2
# 設置啓動按鍵 背景文本爲“RUN” 底色爲“天藍” 字體“Arial” 字體大小“50” 回調函數command 爲【滾動函數】
btn1 = tk.Button(root, text='RUN', bg='lightSkyBlue', font=('Arial', 50), command=round)
#設置按鍵座標
btn1.place(x=800, y=850, width=200, height=200)
# 設置停止按鍵 背景文本爲“RUN” 底色爲“紅色” 字體“Arial” 字體大小“50” 回調函數command 爲【停止函數】
btn2 = tk.Button(root, text='STOP', bg='red', font=('Arial', 50), command=stops)
#設置按鍵座標
btn2.place(x=1000, y=850, width=200, height=200)
# 循環,時刻刷新窗口
root.mainloop()
三、界面展示
抽獎測試
附【Download】:
Dwfish 淹死的魚 2019.1.22