Write a simple small program in python, compile it into exe and run it on win10, pythonwin10

Source: Internet
Author: User

Write a simple small program in python, compile it into 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 ):

  

1 #-*-coding: UTF-8-*-2 from tkinter import * # import Tkinter Library 3 import time, datetime, winsound, threading 4 5 root = Tk () 6 lbl = Label (root, text = "hello, world", width = 30, height = 2) 7 8 def tick (): 9 global timer10 timer = threading. timer (1.0, tick, "") 11 currenttime = time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () 12 lbl. config (text = currenttime) 13 currenttime1 = datetime. datetime. now () 14 if currenttime1.minute = 0 and currenttime1.second = 0 and currenttime1.hour> 5 and currenttime1.hour <23:15 winsound. beep (600) 16 #1000 indicates the sound size, 1000 indicates the occurrence time, and indicates 1 second 17 lbl. pack () # Place the widget in the main window 18 timer. start () 19 20 21 if _ name _ = "_ main _": 22 timer = threading. timer (1.0, tick, "") 23 timer. start () 24 25 26 root. mainloop ()
View Code

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.

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.