Homemade python gadgets (3)——Gadgets1.1

Homemade python gadgets (3) - Gadgets 1.1


1 Introduction

Hello! Hello everyone! I am a stiff neck!
Recently, I have been improving my Gadgets gadgets. Although there is still no progress in terms of functions, in my opinion, comfort is more important than power What does it mean seems to be useless), so I spent a lot of energy learning tkinter

ps:我这里不是来教tkinter的!

Without further ado, let's start


2. Function realization

What I mean is to list the function codes.

2.1 Main program interface

I don’t say this, everyone should know it, just upload the code directly~

from tkinter import *

#这是主界面
root = Tk()
root.title("Gadgets")
root.geometry("768x480")
root.resizable(width=False, height=False)
root.tk.eval("package require Tix")

root.mainloop()

In the end, we can make a screen of 768 pixels * 480 pixels
, which is about this big. I hope everyone can understand how big it is (doge
basic interface


2.1.1 Labels and Buttons

A label is a Label
and a button is a Button.
We need a label as the title and a button for the user to press to confirm the selection
. It's very simple. Let's take a look at the code

......
Title = Label(root, text="Gadgets", font=("Gungsuh", 30))
Title.pack(side=TOP)

Button = Button(root, text="Ready To Start", font=("Gungsuh", 20))
Button.place(x=270, y=200)

root.mainloop()

We have successfully completed the work of buttons and labels. The
generated frame looks like this:
Labels and Buttons
Some people may wonder why there is so much white left below. Let me tell you, this is used to place the selection box.


2.1.2 (Drop-down) selection box

Although this is at least not very useful now, but for the sake of continuous development later, I still added this.
I use the drop-down selection box tkinter.ttk.Combobox, let’s come to Kangkang too.

......
import tkinter.ttk

...
chooseBox = tkinter.ttk.Combobox(root, values=["Extract-Audio"], width=100)
chooseBox.current(0)  # 这是设置选择框里的默认值
chooseBox.pack(anchor="center", expand=True)
...

Quietly show the effect.
Added drop-down selection box interface
The interface of the main program is basically the same, but...
friends who have read the previous articles should know that my program has another program, which is a functional program. Considering it, I still plan to make it an interface.


2.2 Subprogram interface

The interface of the subroutine is similar to that of the main program, but later I found out that doing it is almost equal to not doing it. If you are interested, you can copy the code at the end of the article and try it yourself. If you know how to solve it, please private message me. Thank you very much!

from tkinter import *
from tkinter.tix import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter.messagebox import showwarning
import moviepy.editor as mp


def extract_audio():
    extract_audio_window = Tk()
    extract_audio_window.title("Gadgets")
    extract_audio_window.geometry("512x320")
    extract_audio_window.resizable(width=False, height=False)
    extract_audio_window.tk.eval("package require Tix")

    Title = Label(extract_audio_window, text="Extract Audio", font=("Gungsuh", 30))
    Title.pack(side=TOP)

I don’t run the function here, but I can show you the generated window first
Subroutine window, there are really only so many things

2.2.1 File selection and storage

The difference from the main program is here, we will use the tkinter.filedialogmiddle one askopenfilenameand asksaveasfilename
then I will also add the code for extracting the music

from tkinter import *
from tkinter.tix import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter.messagebox import showwarning
import moviepy.editor as mp


def extract_audio():
    global extract_audio_window
    try:
        extract_audio_window = Tk()
        extract_audio_window.title("Gadgets")
        extract_audio_window.geometry("512x320")
        extract_audio_window.resizable(width=False, height=False)
        extract_audio_window.tk.eval("package require Tix")

        Title = Label(extract_audio_window, text="Extract Audio", font=("Gungsuh", 30))
        Title.pack(side=TOP)

        path = askopenfilename()  # 源文件
        save_path = asksaveasfilename()  # 输出文件的路径以及文件名
        my_clip = mp.VideoFileClip(path)
        my_clip.audio.write_audiofile(f'{
      
      save_path}.mp3')
        extract_audio_window.destroy()

    except OSError:
    	# 如果用户选错了文件或者没有选,会抛出错误,所以我们要对它进行预防处理。
        showwarning("Warning", "Choose the right file")
        extract_audio_window.destroy()

    extract_audio_window.mainloop()

We have basically finished the subroutine, let's continue to look at the part of the main program.


2.3 Event Binding

The function of our button is to allow the user to perform the task selected by the selection box after pressing it. Let's take a look.

2.3.1 Get the content in the selection box

We use get()method

a = chooseBox.get()

aWhat is stored in the variable is the content in the selection box
. We need to judge whether it is in the selection box Extract_audio. If it is, then execute the newly compiled subroutine. If not, send a pop-up window prompt.

from tkinter.messagebox import showinfo
......
def get_choice():
    a = chooseBox.get()
    if a == "Extract-Audio":
        extract_audio()
Button......

Don't forget, the button must bind this function!

Button = Button(root, text="Ready To Start", font=("Gungsuh", 20), command=get_choice)
Button.place(x=270, y=200)

2.4 Abnormal settings of the main program

When the user selects a few characters and deletes a few characters, we have to deal with it, so as not to confuse the user, that is to say, let the program respond a little bit, so ↓

def get_choice():
    a = chooseBox.get()
    if a == "Extract-Audio":
        extract_audio()
    else:
        showinfo("Reminding", "We don't have this survice"
                              ".Please Check Your Choice")

We have added a new one else!
Look at the effect:
exception handling
Up to now, we are almost done, and finally look at the source code.


3. Source code display

3.1 Gadgets 1.1.py

# !/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
import tkinter.ttk
from extract_audio import extract_audio
from tkinter.messagebox import showinfo

# 窗口
root = Tk()
root.title("Gadgets")
root.geometry("768x480")
root.resizable(width=False, height=False)
root.tk.eval("package require Tix")

Title = Label(root, text="Gadgets", font=("Gungsuh", 30))
Title.pack(side=TOP)

chooseBox = tkinter.ttk.Combobox(root, values=["Extract-Audio"], width=100)
chooseBox.current(0)
chooseBox.pack(anchor="center", expand=True)

def get_choice():
    a = chooseBox.get()
    if a == "Extract-Audio":
        extract_audio()
    else:
        showinfo("Reminding", "We don't have this survice"
                              ".Please Check Your Choice")


Button = Button(root, text="Ready To Start", font=("Gungsuh", 20), command=get_choice)
Button.place(x=270, y=200)

root.mainloop()

Extract_audio.py

from tkinter import *
from tkinter.tix import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter.messagebox import showwarning
import moviepy.editor as mp


def extract_audio():
    global extract_audio_window
    try:
        extract_audio_window = Tk()
        extract_audio_window.title("Gadgets")
        extract_audio_window.geometry("512x320")
        extract_audio_window.resizable(width=False, height=False)
        extract_audio_window.tk.eval("package require Tix")

        Title = Label(extract_audio_window, text="Extract Audio", font=("Gungsuh", 30))
        Title.pack(side=TOP)

        path = askopenfilename()
        save_path = asksaveasfilename()
        my_clip = mp.VideoFileClip(path)
        my_clip.audio.write_audiofile(f'{
      
      save_path}.mp3')
        extract_audio_window.destroy()

    except OSError:
        showwarning("Warning", "Choose the right file")
        extract_audio_window.destroy()

    extract_audio_window.mainloop()

Thank you for your patience in reading, Stiff Neck will continue to work hard!

Guess you like

Origin blog.csdn.net/m0_70457107/article/details/126492405