手把手教你从无到有写一个.exe应用小程序,用Python-tkinter写GUI小程序以及程序的打包、Label的动态变化、按钮的命令、图片的导入等等

其他相关文章:PYQT5项目实践|实现Python+cplex 求解模型后,写入GUI小程序中,具体实践包括: CPLEX 的 求解整数规划模型和求解结果以表的形式输出在GUI界面中

Step1 导入tkinter模块

在控制面版(不知道的话windows 键+ R 键)输入:

 pip install tkinter-i https://pypi.tuna.tsinghua.edu.cn/simple

Step2 我的代码

直接给出我的代码,有点小乱,一下午的乱七八糟的学习,哈哈哈,这里面包括了动态改变Label中的Test,test中的字体设置,botton中字体的设置,以及控件的位置等等,我刚开始也遇到一些问题,就是写出来的界面不居中,这个是需要调节的,所以我也有把生成的界面放在屏幕的正中间,还有就是按钮的命令的设置等等。基本内容就是这么多,需要什么在程序中找即可:

import tkinter as tk
from PIL import ImageTk,Image
import random
#初始化Tk()
global myWindow
myWindow = tk.Tk()
#设置标题
myWindow.title('Gift Exchanging')
#设置窗口大小
width = 800
height = 500

global I
I=[]
num = list(range(1, 55))
for i in range(27):
    pair1 = random.choice(num)
    num.remove(pair1)
    pair2 = random.choice(num)
    num.remove(pair2)
    pair = (pair1, pair2)
    I.append(pair)


def create():
    myWindow.destroy()
    global myWindow1
    myWindow1 = tk.Tk()
    # 设置标题
    myWindow1.title('Gift Exchanging_1')
    # 设置窗口大小
    width = 800
    height = 500
    screenwidth = myWindow1.winfo_screenwidth()
    screenheight = myWindow1.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    myWindow1.geometry(alignstr)
    text = tk.StringVar()
    text.set(I[0])
    status = tk.IntVar()
    def pair():
        I.remove(I[0])
        text.set(I[0])
        # if I[0] != None:
        #     L=tk.Text(myWindow1, text=I[0], fg='orange', width=50, height=20, font=('黑体',10)).pack()
        #     I.remove(I[0])
    b1 = tk.Button(myWindow1, text='Pair Begin', command=pair,bg="green", font=('Helvetica 10 bold'), relief='raised',
                   width=20,
                   height=3)
    L = tk.Label(myWindow1, textvariable=text, fg='orange', width=50, height=20, font=('黑体', 180))
    b1.pack(side='top')
    L.pack()
    myWindow1.mainloop()


#设置窗口是否可变长、宽,True:可变,False:不可变
myWindow.resizable(width=False, height=False)
#获取屏幕尺寸以计算布局参数,使窗口居屏幕中央

image2=Image.open(r'New_year.JPG')

background_image = ImageTk.PhotoImage(image2)
w = background_image.width()+200
h = background_image.height()
myWindow.geometry('%dx%d+0+0' % (w,h))
background_label = tk.Label(myWindow, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
#创建两个按钮
b1=tk.Button(myWindow, text='Quit',bg="yellow",font=('Helvetica 10 bold'), relief='raised', width=20, height=3)
b1.pack(side='bottom',expand='y',)
b2=tk.Button(myWindow, text='Begin ', bg="orange",command=create,relief='raised',font=('Helvetica 10 bold'),width=20, height=3)
b2.pack(side='bottom',expand='y')

screenwidth = myWindow.winfo_screenwidth()
screenheight = myWindow.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
myWindow.geometry(alignstr)
#进入消息循环
myWindow.mainloop()

step3 结果

有两个botton,点击Begin即立马进入Pair过程,弹出第二个界面。

在第二个界面,点击Pair Begin,就开始做他们班上的52个同学两两随机配对,点一次配对一次,不重复配对。 

step4 打包

1、导入pyinstaller模块和pywin32模块,导入方式上面已讲。

2、找到所写文件所在目录(图片等放在同一个目录)

在控制面版输入所在目录的路径,如我的所在目录:

则输入:

点击回车,输入dir,就可在空面版看到你所在程序目录,选择你要打包的文件,输入

pyinstaller -F +你要打包的py文件

如:

成功就会显示(画红线处为生成的exe所在路径):

完工!

注:一定要把用到过的图片等复制和exe应用程序一个文件夹里面,不然运行不出来。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码丽莲梦露

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值