一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 用Python給圖像算法做個簡單應用界面

用Python給圖像算法做個簡單應用界面

2021-10-24 10:41xlcaoyi Python

這篇文章主要介紹了用Python給圖像算法做個簡單應用界面,幫助大家更好的理解和學習使用python開發gui,感興趣的朋友可以了解下

以前在Windows上做界面用MFC,現在做算法都是基于Python,所以轉用Python的Tkinter庫來做。主要是能使用Opencv和Torch處理數據,然后在界面上顯示。

效果如下:

主要包括3個板塊,其余還有一些小功能:

1、顯示固定的圖片。或從電腦加載一張圖片并顯示(涉及到按鈕的響應函數編寫和彈窗)

2、下拉框和文本框的使用

3、進度條的使用(涉及到多線程)

用Python給圖像算法做個簡單應用界面

用Python給圖像算法做個簡單應用界面

Tkinter支持控件自動調整布局,但是時間比較趕就不研究了,使用固定位置布局,界面也不給調整。

控件名稱

  • Buttom 按鈕,軟件交互功能實現
  • Label (叫什么不重要),用來顯示圖片或文字
  • ComboBox 下拉框,做選擇
  • Entry 文本框,做文本輸入
  • Progressbar 進度條,算法跑起來之后顯示進度
  • LabelFrame (...),灰色的框框,模塊化布局控件

代碼如下:

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox
import tkinter.filedialog
import cv2 as cv
from PIL import Image, ImageTk
import time
import threading
 
RELIEF=["flat", "raised", "sunken", "solid", "ridge", "groove"]
CURSOR=["arrow","circle","clock","cross","dotbox","exchange",
        "fleur","heart","man","mouse","pirate","plus",
        "shuttle","sizing","spider","spraycan","star","target",
        "tcross","trek","watch"]
 
def PIL2CV(im):
    im = im[:, :, ::-1]
    return ImageTk.PhotoImage(Image.fromarray(im))
 
def Buttom1_CallBack():
    filename = tk.filedialog.askopenfilename() #彈出文件選擇對話框
    if filename=="": #用戶沒有選擇任何文件
        return
    new_img = cv.imread(filename)
    if new_img is None:
        tk.messagebox.showerror("抱歉", "圖片加載失敗!")
        return
    new_img = cv.resize(new_img, (130, 120))
    new_img = PIL2CV(new_img)
    #后面兩句實現圖片切換顯示
    Label2.configure(image=new_img, width=130, height=120)
    Label2.image = new_img
    tk.messagebox.showinfo("提示","加載圖片完成!")
 
def Buttom2_CallBack():
    info = Combobox1.get()
    param = Entry1.get()
    tk.messagebox.showwarning("警告", "你選擇了:"+info+" "+param)
 
def process_code(delay):
    for i in range(100):
        Progressbar1["value"] = i+1
        root.update()
        time.sleep(delay)
    Buttom3.configure(text="開始處理", state="normal")
    tk.messagebox.showinfo("提示", "處理完成!")
    Progressbar1.configure(value=0)
 
def Buttom3_CallBack():
    yn = tk.messagebox.askyesno("警告","是否需要開始處理?")
    if not yn:
        return
 
    Buttom3.configure(text="處理中...", state="disabled") #控件失效
    delay = 0.01
 
    # 單獨開一個線程,綁定線程函數process_code,參數后面的","很關鍵
    # 不開線程界面會進入處理函數死循環,用戶體驗不太好
    t = threading.Thread(target=process_code, args=(delay,))
    t.start()
 
def Buttom4_CallBack():
    global page_count
    if page_count<=0:
        page_count = 0
        return
    else:
        page_count -= 1
        Label4.configure(text="第"+str(page_count)+"頁")
    return
 
def Buttom5_CallBack():
    global page_count
    if page_count>=100:
        page_count = 100
        return
    else:
        page_count += 1
        Label4.configure(text="第" + str(page_count) + "頁")
    return
 
#上面是控件的響應函數
################################################################################
#下面是界面控件的布局
 
#主界面
root = tk.Tk()
root.title("python界面測試") #修改界面標題
root.iconbitmap("img/tm.ico") #修改界面ico
root.geometry("800x500") #設定界面尺寸 HxW
root.resizable(width=False, height=False) #不允許調整窗口大小,不固定刪除此行
 
#添加兩個板塊邊界框
Frame1 = tk.LabelFrame(root, height=200, width=145)
Frame1.place(x=15, y=100)
Frame2 = tk.LabelFrame(root, text="結果顯示", height=400, width=620)
Frame2.place(x=170, y=5)
 
#添加圖片顯示框、加載圖片框、加載圖片按鈕
img = cv.imread("img/title.jpg") #opencv加載圖片
img = cv.resize(img, (140,70)) #圖片縮放
img = PIL2CV(img) #opencv格式轉pillow
Label1 = tk.Label(root, image=img) #初始化默認圖片
Label1.place(x=15, y=20) #圖片顯示框在界面上的位置
 
Label2 = tk.Label(root,
                  width=18,height=7, #控件大小(注意單位不是像素)
                  bg="white") #默認白色背景
Label2.place(x=20,y=110) #圖片顯示框在界面上的位置
 
Buttom1 = tk.Button(root,
                    width=15,height=1, #按鈕大小
                    text="加載檢索圖片", #按鈕文本
                    relief=RELIEF[3], #按鈕的風格
                    command=Buttom1_CallBack) #綁定響應函數
Buttom1.place(x=25, y=250) #按鈕在界面上的位置
 
#添加參數文本框、下拉框、下拉框內容輸出按鈕
Combobox1 = ttk.Combobox(root, width=17, height=1)
Combobox1["value"] = ("窗前明月光","疑是地上霜","舉頭望明月","明月照我影")
Combobox1.current(0)
Combobox1.place(x=15, y=320)
 
Label3 = tk.Label(root, text="參數")
Label3.place(x=15, y=350)
 
Entry1 = ttk.Entry(root, width=9) #文本框為啥沒有H
Entry1.place(x=50, y=350)
Entry1.insert(0,"0.5")
 
Buttom2 = tk.Button(root,
                    width=15,height=1,
                    text="你選擇了什么?",
                    relief=RELIEF[3],
                    command=Buttom2_CallBack)
Buttom2.place(x=25, y=380)
 
#添加進度條、開始處理按鈕
Progressbar1 = ttk.Progressbar(root, length=600, value=0, cursor=CURSOR[1])
Progressbar1.place(x=15, y=460)
 
Buttom3 = tk.Button(root,
                    width=15,height=1,
                    text="開始處理",
                    relief=RELIEF[3],
                    command=Buttom3_CallBack)
Buttom3.place(x=630, y=455)
 
#添加兩個滾動按鈕
Buttom4 = tk.Button(root,
                    width=3,height=1,
                    text="<",
                    relief=RELIEF[1],
                    command=Buttom4_CallBack)
Buttom4.place(x=380, y=410)
 
global page_count #全局變量,用來控制頁碼
page_count=0
Label4 = tk.Label(root, text="第0頁")
Label4.place(x=420, y=410)
 
Buttom5 = tk.Button(root,
                    width=3,height=1,
                    text=">",
                    relief=RELIEF[1],
                    command=Buttom5_CallBack)
Buttom5.place(x=470, y=410)
 
root.mainloop()
#這句話后面不能有代碼

以上就是用Python給圖像算法做個簡單應用界面的詳細內容,更多關于python 應用界面的資料請關注服務器之家其它相關文章!

原文鏈接:https://blog.csdn.net/XLcaoyi/article/details/116270230

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 秋霞理论一级在线观看手机版 | 国产成+人+综合+亚洲不卡 | 无码爽死成人777在线观看网站 | 亚洲午夜性春猛交xxxx | a级片在线播放 | 国产首页精品 | 欧美精品v日韩精品v国产精品 | 色yeye在线观视频 | 精品国产自在现线拍400部 | 91精品国产麻豆国产自产在线 | 欧美一级视频免费观看 | 日本高清全集免费观看 | 日本特黄一级午夜剧场毛片 | 国产精品一区二区三区免费 | 国产馆在线观看免费的 | 精品国产成人高清在线 | 男人天堂网页 | 精品久久香蕉国产线看观看亚洲 | 欧美一级特黄刺激大片视频 | 亚洲成人aa| 久久视频在线视频观看天天看视频 | 扒开斗罗美女了的胸罩和内裤漫画 | 国产裸露片段精华合集链接 | 国产一级在线观看 | 色哟哟哟在线精品观看视频 | 免费看美女被靠到爽的视频 | 沉沦艳妇杨幂肉体小说 | 草莓视频旧版 | 好深快点再快点好爽视频 | 44444色视频在线观看 | 91网红福利精品区一区二 | 四虎成人4hutv影院 | 我被男人下药添得好爽 | 天天爱天天操天天射 | 久久免费看少妇级毛片蜜臀 | 91caoporm在线进入 | 女人扒开下面让男人桶爽视频 | 欧美日韩综合网在线观看 | 好看的亚洲视频 | 毛片一区二区三区提莫影院 | а天堂中文最新版在线 |