Compile a simple program in python and compile it into an exe and run it on win10, pythonwin10
Every day's work is actually boring. I knew IT companies should be involved. The most recent work is to check the customer flow data from to, sort the data in the form, and hand it over to the blank online, submitted to the lead for consultation.
After all, human energy is limited, so it is not always possible to do this job in a timely manner at every hour. The Brainwave of this kind of meticulous work should be done by the calculator to tell me how to work at every hour.
Just do it. Usually I only use c # To write a small program. Because the office computer is public, I didn't want to install visual studio like my computer. I just wanted to try python. I have always heard of the famous saying "I use python for a short life ". This is my entry point.
My requirements are actually very simple. Let the program get the current time, and then use the timer with an interval of 1 second to refresh the time repeatedly. When the time is scrolled to the entire point, and the whole point is within our expected range [7-23]. I asked the computer to beep and prompt that it was time to check the passenger flow.
After a simple query, I decided to use tkinter for the UI. I had low requirements on the UI and had poor design, so I just needed to put a Lable in the window, let the Lable text display the current time in seconds.
Step 1: Use tkinter to draw a form and add a Lable on the form, as shown below:
Next, Step 2: Use a timer for timing, set interval to 1000 ms, implement it in start () time, get the current time value, and then assign it to the text attribute of Lable, in this way, the scroll effect similar to the clock is achieved. As follows:
The previous two steps have been successfully implemented, and the code remains one step. The last step is to determine whether the obtained current time is the hour during the start () event. If it is the expected hour, let the computer beep for a while to remind me that I should go to work. This is mainly a new function, how to make the calculator beep.
The Code is as follows (python 2.7 ):
#-*-Coding: UTF-8-*-from tkinter import * # import the Tkinter library import time, datetime, winsound, threadingroot = Tk () lbl = Label (root, text = "hello, world", width = 30, height = 2) def tick (): global timer = threading. timer (1.0, tick, "") currenttime = time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () lbl. config (text = currenttime) currenttime1 = datetime. datetime. now () if currenttime1.minute = 0 and currenttime1.second = 0 and currenttime1.hour> 5 and currenttime1.hour <23: winsound. beep (600) #1000 indicates the sound size, 1000 indicates the occurrence time, and indicates the 1 second lbl. pack () # Place the widget in the main window timer. start () if _ name _ = "_ main _": timer = threading. timer (1.0, tick, "") timer. start () root. mainloop ()
None of the above is the focus of my record today. The record in this article focuses on trying to compile the py code into an exe file.
Install pyinstaller
Use pyinstaller to compile the py file into exe in the specified directory.
First, complete the first step. After installing python, complete the environment variable configuration. I will not go into details here. Please refer to the blog. Here is a brief introduction to installing pyinstaller on cmd. Why use it? Please contact Baidu. I am also confused. Installation command: pip install pyinstaller. The installation process is as follows:
Next, let's take the second step. Today, I took a lot of detours in this step, and finally got a barely usable compromise after I checked many materials. If you have a better method, please do not hesitate to advise.
Copy the prepared py file to the following path:
Run the following command to compile the file into an exe:
Pyinstaller-F-w yourfilename. py
The exe file is compiled and the file structure is generated.
Now, I have finished my work today. Compile a scheduled reminder program in python.
Here I will record two questions:
First, I feel that the exe file compiled by pyinstaller is too large, and there are more than 8 Mb. Why?
Second, if the file is not compiled into exe under the specified directory, it will not succeed. Why? Attached failure:
Finally, I will leave a little detail here for future extension. The next time I try to use tts technology, I would like to remind you that the current beep sound is a bit silly.