博客原文【不好吃の蛋蛋】
完整代碼
在Python實(shí)現(xiàn)自動(dòng)掛機(jī)腳本(基礎(chǔ)篇)中我們實(shí)現(xiàn)了簡(jiǎn)單的掛機(jī)腳本,但這樣的腳本運(yùn)行起來比較麻煩,也沒有好看的界面。本篇中,我們將使用tkinter設(shè)計(jì)GUI界面,并用pyinstaller打包成.exe文件
先上圖
tkinter
tkinter是Python內(nèi)置的GUI設(shè)計(jì)界面,對(duì)小白來說容易上手,你也可以嘗試用pyqt或者wx
關(guān)于tkinter可以看一下莫煩教程
首先創(chuàng)建一個(gè)窗口,并設(shè)置必要信息
import tkinter as tk
from icon import img
window = tk.Tk()
window.title('奴良小軒v0.1')
window.geometry('240x480+120+30')
with open('tmp.ico', 'wb+') as fp:
fp.write(base64.b64decode(img))
window.iconbitmap('tmp.ico')
os.remove('tmp.ico')
label = tk.Label(window, font=('微軟雅黑', 12),
text='請(qǐng)將PC端陰陽師調(diào)節(jié)與小寶等高')
label.pack()
設(shè)置圖標(biāo)
默認(rèn)情況下,窗口圖標(biāo)是紅色的TK
,想修改則使用.iconbitmap(path)方法,但是,在實(shí)際使用踩坑了。因?yàn)楹竺嫖視?huì)使用pyinstaller打包,因?yàn)檎也坏絧ath路徑運(yùn)行程序會(huì)報(bào)錯(cuò),找了好久才找到這個(gè)錯(cuò)誤。
解決方案是先將圖標(biāo)讀取并寫入ico.py
文件,調(diào)用.iconbitmap(path)時(shí)讀取ico.py
,代碼如下:
import base64
open_icon = open('yaodao.ico', 'rb')
b64str = base64.b64encode(open_icon.read())
open_icon.close()
write_data = "img = '%s'" % b64str
f = open('icon.py', 'w+')
f.write(write_data)
f.close()
功能選擇
fun_var = tk.IntVar()
fun_text = ''
def print_selection():
global fun_text
if fun_var.get() == 1:
fun_text = '寮突破'
elif fun_var.get() == 2:
fun_text = '御靈、業(yè)原火'
elif fun_var.get() == 3:
fun_text = '魂十隊(duì)員(未完成)'
elif fun_var.get() == 4:
fun_text = '魂十隊(duì)長(zhǎng)(未完成)'
elif fun_var.get() == 5:
fun_text = '狗糧隊(duì)員(未完成)'
label.config(text='功能選擇: ' + fun_text)
rb1 = tk.Radiobutton(window, text='寮突破', font=('微軟雅黑', 10),
variable=fun_var, value=1, command=print_selection)
rb1.place(x=15, y=30)
rb2 = tk.Radiobutton(window, text='御靈、業(yè)原火', font=('微軟雅黑', 10),
variable=fun_var, value=2, command=print_selection)
rb2.place(x=15, y=60)
rb3 = tk.Radiobutton(window, text='魂十隊(duì)員', font=('微軟雅黑', 10),
variable=fun_var, value=3, command=print_selection)
rb3.place(x=15, y=90)
rb4 = tk.Radiobutton(window, text='魂十隊(duì)長(zhǎng)', font=('微軟雅黑', 10),
variable=fun_var, value=4, command=print_selection)
rb4.place(x=15, y=120)
rb5 = tk.Radiobutton(window, text='狗糧隊(duì)員', font=('微軟雅黑', 10),
variable=fun_var, value=5, command=print_selection)
rb5.place(x=15, y=150)
- 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
- 29
- 30
- 31
- 32
- 33
- 34
- 35
開始按鈕
- start_mission()中定義了每一個(gè)功能所要執(zhí)行的函數(shù),注意的是,獨(dú)立功能需要放在一個(gè)線程中執(zhí)行,不然界面會(huì)被阻塞卡死
- 全局變量
is_start
用來控制功能的執(zhí)行與停止 - click()函數(shù)用來改變按鈕顯示以及鎖定功能選擇
rb_list = [rb1, rb2, rb3, rb4, rb5]
button_var = tk.StringVar()
button_var.set('開始')
is_click = False
def start_mission():
global is_start
if fun_var.get() == 1:
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 開始執(zhí)行寮突破\n')
text.see('end')
window_size = get_window_info()
if window_size:
window.geometry('240x480+%d+%d' % (window_size[0]-240, window_size[1]))
is_start = True
thread1 = threading.Thread(target=liao_tupo, args=(window_size,))
thread1.start()
elif fun_var.get() == 2:
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 開始執(zhí)行御靈、業(yè)原火\n')
text.see('end')
window_size = get_window_info()
if window_size:
window.geometry('240x480+%d+%d' % (window_size[0] - 240, window_size[1]))
is_start = True
thread2 = threading.Thread(target=yu_ling, args=(window_size,))
thread2.start()
elif fun_var.get() == 3:
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 魂十隊(duì)員功能未開發(fā)\n')
text.see('end')
elif fun_var.get() == 4:
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 魂十隊(duì)長(zhǎng)功能未開發(fā)\n')
text.see('end')
elif fun_var.get() == 5:
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 狗糧隊(duì)員功能未開發(fā)\n')
text.see('end')
def stop_mission():
global is_start
is_start = False
text.insert('end', strftime('%H:%M:%S', localtime()) + ' 停止執(zhí)行\(zhòng)n')
text.see('end')
def click():
global is_click
if not is_click:
is_click = True
button_var.set('停止')
label.config(text=fun_text + ' 已經(jīng)開始')
for rb in rb_list:
rb.config(state='disabled')
button_adjust.config(state='disabled')
start_mission()
else:
is_click = False
button_var.set('開始')
label.config(text=fun_text + ' 已經(jīng)停止')
for rb in rb_list:
rb.config(state='active')
button_adjust.config(state='active')
stop_mission()
button = tk.Button(window, textvariable=button_var, width=10,
height=1, command=click)
button.place(x=140, y=60)
- 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
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
文本顯示
import ScrolledText
text = ScrolledText.ScrolledText(window, width=29, height=17)
text.place(x=15, y=180)
注意的一點(diǎn)是,再每次輸出文本的時(shí)候希望自動(dòng)顯示低端,這時(shí)需要在insert之后執(zhí)行text.see('end')
Pyinstaller打包
pyinstaller -F -w -i ./yaodao.ico ./tk_gui.py
-F
表示輸出單文件exe-w
表示不顯示命令行-i
設(shè)置圖標(biāo)
更多參數(shù)設(shè)置詳見這里
至此全部搞定,打開exe時(shí)記得右鍵管理員權(quán)限打開