分享

Python实现自动挂机脚本(GUI & 打包)

 风声之家 2018-06-18

Python实现自动挂机脚本(GUI & 打包)

博客原文【不好吃の蛋蛋】 
完整代码

Python实现自动挂机脚本(基础篇)中我们实现了简单的挂机脚本,但这样的脚本运行起来比较麻烦,也没有好看的界面。本篇中,我们将使用tkinter设计GUI界面,并用pyinstaller打包成.exe文件 
先上图 

tkinter

tkinter是Python内置的GUI设计界面,对小白来说容易上手,你也可以尝试用pyqt或者wx 
关于tkinter可以看一下莫烦教程

首先创建一个窗口,并设置必要信息

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='请将PC端阴阳师调节与小宝等高')  # 显示一段文本
label.pack()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

设置图标

默认情况下,窗口图标是红色的TK,想修改则使用.iconbitmap(path)方法,但是,在实际使用踩坑了。因为后面我会使用pyinstaller打包,因为找不到path路径运行程序会报错,找了好久才找到这个错误。 
解决方案是先将图标读取并写入ico.py文件,调用.iconbitmap(path)时读取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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

功能选择

# Radiobutton #
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 = '御灵、业原火'
    elif fun_var.get() == 3:
        fun_text = '魂十队员(未完成)'
    elif fun_var.get() == 4:
        fun_text = '魂十队长(未完成)'
    elif fun_var.get() == 5:
        fun_text = '狗粮队员(未完成)'
    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='御灵、业原火', font=('微软雅黑', 10),
                     variable=fun_var, value=2, command=print_selection)
rb2.place(x=15, y=60)
rb3 = tk.Radiobutton(window, text='魂十队员', font=('微软雅黑', 10),
                     variable=fun_var, value=3, command=print_selection)
rb3.place(x=15, y=90)
rb4 = tk.Radiobutton(window, text='魂十队长', font=('微软雅黑', 10),
                     variable=fun_var, value=4, command=print_selection)
rb4.place(x=15, y=120)
rb5 = tk.Radiobutton(window, text='狗粮队员', font=('微软雅黑', 10),
                     variable=fun_var, value=5, command=print_selection)
rb5.place(x=15, y=150)
# Radiobutton #
  • 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()中定义了每一个功能所要执行的函数,注意的是,独立功能需要放在一个线程中执行,不然界面会被阻塞卡死
  • 全局变量is_start用来控制功能的执行与停止
  • click()函数用来改变按钮显示以及锁定功能选择
# button start#
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()) + ' 开始执行寮突破\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()) + ' 开始执行御灵、业原火\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()) + ' 魂十队员功能未开发\n')
        text.see('end')  # 自动显示底部
    elif fun_var.get() == 4:
        text.insert('end', strftime('%H:%M:%S', localtime()) + ' 魂十队长功能未开发\n')
        text.see('end')  # 自动显示底部
    elif fun_var.get() == 5:
        text.insert('end', strftime('%H:%M:%S', localtime()) + ' 狗粮队员功能未开发\n')
        text.see('end')  # 自动显示底部


def stop_mission():
    global is_start
    is_start = False
    text.insert('end', strftime('%H:%M:%S', localtime()) + ' 停止执行\n')
    text.see('end')  # 自动显示底部


def click():
    global is_click
    if not is_click:
        is_click = True
        button_var.set('停止')
        label.config(text=fun_text + ' 已经开始')
        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 + ' 已经停止')
        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)
# button start#
  • 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 = tk.Text(window, width=29, height=17)  # 输出文本框
text.place(x=15, y=180)
  • 1
  • 2
  • 3
  • 4

注意的一点是,再每次输出文本的时候希望自动显示低端,这时需要在insert之后执行text.see('end')

Pyinstaller打包

pyinstaller -F -w -i ./yaodao.ico ./tk_gui.py
  • 1
  • -F表示输出单文件exe
  • -w表示不显示命令行
  • -i设置图标

更多参数设置详见这里

至此全部搞定,打开exe时记得右键管理员权限打开

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约