Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a python program for yourself

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to write a python program for your own use". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write a python program for your own use.

Interface is also a tool, so is there an interface tool library? There have to be, and there are many, you can search "python interface library" to see a lot of this introduction, and I would like to introduce, python has the earliest tkinter, through it to achieve the interface program.

Tkinter is built into python, so you don't have to install it, just import and use it.

(1) TTS's GUI program from tkinter import *

Import tkinter.messagebox as msgbox

Import tkinter.filedialog as filedlg

Import os

Import pyttsx3

From pydub import AudioSegment

Import threading

Txtfile = ""

Window = Tk ()

Pathlabel = Label (window, text= "...")

Def fileFunc ():

Default_dir = "file path"

Global txtfile

Global pathlabel

Txtfile = filedlg.askopenfilename (title= "Select File", initialdir= (os.path.expanduser (default_dir)

(path, fname) = os.path.split (txtfile)

Pathlabel ["text"] = fname

Def converThreadFunc (content):

Outfile = "out.aiff"

Tts = pyttsx3.init ()

Tts.save_to_file (content, outfile)

Tts.runAndWait ()

Def convertFunc ():

If len (txtfile) = = 0:

Msgbox.showinfo ("prompt", "Please select a text file first")

Return

Content = open (txtfile, "r") .read ()

If len (content) = = 0:

Msgbox.showinfo ("prompt", "text file has no content, conversion terminates, no voice file is output")

Return

T1 = threading.Thread (target=converThreadFunc, args= (content,))

T1.start ()

T1.join ()

Outfile = "out.aiff"

AudioSegment.from_file (outfile) .export ("out.mp3", format= "mp3")

Msgbox.showinfo ("prompt", "converted successfully, out.mp3 in the program directory is the final voice file:% s"% os.getcwd ())

Os.system ("open'% s"% os.getcwd ())

Def main ():

Window.title ("TTS- text to speech")

Window.geometry ("320x320+100+100")

Filebtn = Button (window, text= "Select text file", command=fileFunc)

Convertbtn = Button (window, text= "convert to voice", command=convertFunc)

Filebtn.place (Xerox 10, YB10)

Pathlabel.place (Xerox 10, YB40)

Convertbtn.place (Xerox 10, YBG 80)

Window.mainloop ()

If _ _ name__== "_ _ main__":

Main ()

Get to the point.

1, the use of tkinter controls, messagebox is a prompt box, filedialog is a file selection control, such as Button and Label, these are commonly used controls, their creation and property settings, there are a lot of details, you can search to understand.

2. Here, the place method is used to set the location of the control, and similar methods include pack, grid, and so on.

3. In terms of business logic, the focus is on dealing with the click events of the two buttons, that is, the xxFunc functions from the two def.

4, text-to-speech processing, using the code introduced in the previous section, there is no mystery.

5. Pyttsx3.engine 's function runAndWait will end the current thread after execution, so you can't execute this function in the main thread, that is, the UI thread, but start a thread to execute it, otherwise the main interface will disappear after runAndWait execution, which is not expected.

A screenshot after running looks like this:

The program is ready, regardless of whether it is ugly or not, at least the function can be used. If you use it yourself, how can you start this program quickly?

(2) call the program with alfred

I have described how to quickly switch or start a program on a mac system. The tool used is alfred, which is a super useful tool. Now use alfred again to enable the program you just wrote.

With regard to the installation or use of alfred, there are many introductions on the Internet, as well as when I introduced the "mac environment and tools". However, there is a place to pay attention to: it is possible to use the terminal to execute the above python program, but once the python program is executed through alfred, it will be abnormal in AudioSegment.form_file. In the end, export will not produce a voice file, and even if you execute the ffmpeg command directly in the python code, it will fail. When alfred executes the python script, AudioSegment (including the ffmpeg it uses) does not have permission to access the file. With this in mind, it's time for a workaround: let alfred open a new terminal and let the terminal execute the python program-- create tools in this way.

1. Create a new "Script Filter" on alfred:

2. The new terminal will execute the cmd.sh script, which looks like this:

Cd'/ Users/freejet/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows/user.workflow.4D91A493-39F3-433C-9D33-65917236A6F5'

Source ~ / Desktop/pyenv/bin/activate

Python3 demo_gui.py

First, cd to the directory where the alfred script workflow is located, and you can find this directory path by right-clicking workflow, then activate the python3 environment (introduced in the first section), and finally execute the python program, which is the "TTS GUI program" introduced above.

Put the cmd.sh in the current workflow directory so that alfred can find it.

3. Execute it, and the effect is as follows:

The last out.mp3 is the file you finally want.

The corresponding text looks like this:

Yao grass a he Bi, spring into Wuling Creek. Countless peach blossoms on the stream, there are orioles on the flowers, I want to wear flowers to find my way, straight into the depths of the white clouds, mighty rainbow. Only afraid of flowers deep, red dew wet clothes. Sit on jade, jade pillow, brush gold emblem. Where is the fairy, heaven and man accompany me with the white snail cup. I am Ganoderma lucidum fairy grass, not for Zhu Lian Dan face, Changxiao also what to do! Get drunk and dance down the mountain, and the bright moon goes home. Song. Huang Tingjian sings a song with water. Sightseeing "

At this point, I believe you have a deeper understanding of "how to write a python program for your own use". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report