新增code
This commit is contained in:
parent
eaa592389b
commit
5fb4d78dbc
139
CLASS/code/test_class_0711.py
Normal file
139
CLASS/code/test_class_0711.py
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
|
||||||
|
# ------------------- 繼承 -------------------------
|
||||||
|
print(f'------------------- 繼承 -------------------------')
|
||||||
|
class Lab_location:
|
||||||
|
def __init__(self):
|
||||||
|
self.location = "EL125"
|
||||||
|
|
||||||
|
|
||||||
|
class Lab_student(Lab_location):
|
||||||
|
def __init__(self,student):
|
||||||
|
super().__init__()
|
||||||
|
self.student = student
|
||||||
|
|
||||||
|
|
||||||
|
def lab_detail(self):
|
||||||
|
print(f'student : {self.student}') # 原本定義的
|
||||||
|
print(f'Lab_位置 : {self.location}') # 可以從Lab_location 抓出物件
|
||||||
|
|
||||||
|
student1 = Lab_student("M11112030")
|
||||||
|
student1.lab_detail()
|
||||||
|
|
||||||
|
# ---------------------封裝---------------------------
|
||||||
|
print(f'------------------- 封裝 -------------------------')
|
||||||
|
|
||||||
|
class Lab_student:
|
||||||
|
def __init__(self,name):
|
||||||
|
self.name = name
|
||||||
|
self.profession = "警察"
|
||||||
|
|
||||||
|
def student_name(self):
|
||||||
|
print(f'name : {self.name}')
|
||||||
|
|
||||||
|
|
||||||
|
def crime(self):
|
||||||
|
print(f'{self.name} 要來逮捕你了')
|
||||||
|
|
||||||
|
def __no_crime(self):
|
||||||
|
print(f'{self.name} 從你身邊路過')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
studen1 = Lab_student("王希名")
|
||||||
|
studen1.student_name()
|
||||||
|
studen1.crime()
|
||||||
|
#studen1.__no_crime()
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------多型-----------------------
|
||||||
|
|
||||||
|
print(f'------------------- 多型 -------------------------')
|
||||||
|
class Lab_location:
|
||||||
|
def __init__(self):
|
||||||
|
self.location = "EL125"
|
||||||
|
|
||||||
|
class A1_student(Lab_location):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
def lab_detail(self):
|
||||||
|
print(f'M11112030 在 {self.location}')
|
||||||
|
|
||||||
|
class A2_student(Lab_location):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
def lab_detail(self):
|
||||||
|
print(f'M11112031 在 {self.location}')
|
||||||
|
|
||||||
|
class A3_student(Lab_location):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
|
def lab_detail(self):
|
||||||
|
print(f'M11112032 在 {self.location}')
|
||||||
|
|
||||||
|
student1 = A1_student()
|
||||||
|
student2 = A2_student()
|
||||||
|
student3 = A3_student()
|
||||||
|
|
||||||
|
student1.lab_detail()
|
||||||
|
student2.lab_detail()
|
||||||
|
student3.lab_detail()
|
||||||
|
|
||||||
|
# ----------------多型---------------
|
||||||
|
print(f'------------------- 多型 -------------------------')
|
||||||
|
class Lab_location:
|
||||||
|
def __init__(self):
|
||||||
|
self.location = "EL125"
|
||||||
|
|
||||||
|
class Lab_student(Lab_location):
|
||||||
|
def __init__(self,name):
|
||||||
|
super().__init__()
|
||||||
|
self.name = name
|
||||||
|
def lab_detail(self):
|
||||||
|
print(f'{self.name} 在 {self.location}')
|
||||||
|
|
||||||
|
student1 = Lab_student("A")
|
||||||
|
student2 = Lab_student("B")
|
||||||
|
student3 = Lab_student("C")
|
||||||
|
|
||||||
|
student1.lab_detail()
|
||||||
|
student2.lab_detail()
|
||||||
|
student3.lab_detail()
|
||||||
|
|
||||||
|
# --------------------------------class-----------------------
|
||||||
|
print(f'------------------- class -------------------------')
|
||||||
|
class Person():
|
||||||
|
def __init__(self,name,age,weight,high):
|
||||||
|
self.eye = 2
|
||||||
|
self.hand = 2
|
||||||
|
self.noise = 1
|
||||||
|
self.leg = 2
|
||||||
|
self.money =0
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
self.weight = weight
|
||||||
|
self.high = high
|
||||||
|
def person_body_detail(self):
|
||||||
|
print(f'眼睛數量 : {self.eye}')
|
||||||
|
print(f'手數量 : {self.hand}')
|
||||||
|
print(f'鼻子數量 : {self.noise}')
|
||||||
|
print(f'腿數量 : {self.leg}')
|
||||||
|
def person_introduce(self):
|
||||||
|
high = float(self.high/100)
|
||||||
|
print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}')
|
||||||
|
|
||||||
|
|
||||||
|
test_1 = Person(name='王希銘',age = 37,weight=105,high=168)
|
||||||
|
test_1.money = 20000
|
||||||
|
test_1.person_body_detail()
|
||||||
|
test_1.person_introduce()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
Thread/code/img/1.jpg
Normal file
BIN
Thread/code/img/1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
Thread/code/img/2.jpg
Normal file
BIN
Thread/code/img/2.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
BIN
Thread/code/img/3.jpg
Normal file
BIN
Thread/code/img/3.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
BIN
Thread/code/img/66406109.jpg
Normal file
BIN
Thread/code/img/66406109.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 66 KiB |
111
Thread/code/test_Qthread_0716_main.py
Normal file
111
Thread/code/test_Qthread_0716_main.py
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
import cv2
|
||||||
|
import sys, time, os
|
||||||
|
import random
|
||||||
|
import numpy as np
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
from test_Qthread_0716_ui import Ui_MainWindow
|
||||||
|
from PyQt5.QtCore import QDate,QTime
|
||||||
|
from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel,QMainWindow, QWidget, QPushButton
|
||||||
|
from PyQt5.QtCore import QThread, pyqtSignal
|
||||||
|
|
||||||
|
|
||||||
|
class ReadTime(QtCore.QThread): # 讀取時間
|
||||||
|
time_out = pyqtSignal(str) # 聲明一個帶字串參數的信號
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
result = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 讀取當下時間
|
||||||
|
self.time_out.emit(f'{result}') # 傳送信号
|
||||||
|
self.msleep(500) # 休眠0.5秒
|
||||||
|
|
||||||
|
|
||||||
|
class img_out(QtCore.QThread):
|
||||||
|
img_data = QtCore.pyqtSignal(np.ndarray)
|
||||||
|
data=pyqtSignal(str)
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.img_path_list=['img/1.jpg','img/2.jpg','img/3.jpg']
|
||||||
|
def run(self):
|
||||||
|
data_list=["stone","scissors","cloth"]
|
||||||
|
while True:
|
||||||
|
img_num = random.randint(0,len(self.img_path_list)-1)
|
||||||
|
img = cv2.imread(self.img_path_list[img_num])
|
||||||
|
self.img_data.emit(img)
|
||||||
|
self.data.emit(data_list[img_num])
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 轉Qlabel的格式
|
||||||
|
def label_to_view(img): # 原圖
|
||||||
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
|
||||||
|
Ny, Nx, _ = img.shape
|
||||||
|
img = QtGui.QImage(img.data, Nx, Ny, Nx * 3, QtGui.QImage.Format_RGB888) # 須改格式
|
||||||
|
return img
|
||||||
|
|
||||||
|
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||||
|
def __init__(self, parent=None): #按鍵設定
|
||||||
|
super(MainWindow, self).__init__(parent)
|
||||||
|
self.setupUi(self)
|
||||||
|
|
||||||
|
self.time = ReadTime()
|
||||||
|
self.time.start()
|
||||||
|
self.time.time_out.connect(self.settimeText)
|
||||||
|
|
||||||
|
self.person_1 = ""
|
||||||
|
self.person_2 = ""
|
||||||
|
|
||||||
|
self.img_out_1 = img_out()
|
||||||
|
self.img_out_1.start()
|
||||||
|
self.img_out_1.img_data.connect(self.show_view_1)
|
||||||
|
self.img_out_1.data.connect(self.show_data_1)
|
||||||
|
|
||||||
|
self.img_out_2 = img_out()
|
||||||
|
self.img_out_2.start()
|
||||||
|
self.img_out_2.img_data.connect(self.show_view_2)
|
||||||
|
self.img_out_2.data.connect(self.show_data_2)
|
||||||
|
|
||||||
|
|
||||||
|
def settimeText(self, str_in):
|
||||||
|
str_in = f'<span style=" margin-right:3px ;color:blue; font-size: 18px ; font-weight: bold;">{str_in}</span>'
|
||||||
|
self.view_time.setText(str_in)
|
||||||
|
|
||||||
|
def show_view_1(self,img):
|
||||||
|
img = label_to_view(img)
|
||||||
|
self.view_1.setPixmap(QtGui.QPixmap.fromImage(img))
|
||||||
|
|
||||||
|
def show_view_2(self,img):
|
||||||
|
img = label_to_view(img)
|
||||||
|
self.view_2.setPixmap(QtGui.QPixmap.fromImage(img))
|
||||||
|
|
||||||
|
def show_data_1(self,str):
|
||||||
|
self.person_1 = str
|
||||||
|
self.who_win()
|
||||||
|
def show_data_2(self,str):
|
||||||
|
self.person_2 = str
|
||||||
|
self.who_win()
|
||||||
|
def who_win(self):
|
||||||
|
if self.person_1=="scissors" and self.person_2 =="cloth":
|
||||||
|
self.textEdit.setText("Person_1_Win")
|
||||||
|
if self.person_1=="stone" and self.person_2 =="scissors":
|
||||||
|
self.textEdit.setText("Person_1_Win")
|
||||||
|
if self.person_1=="cloth" and self.person_2 =="stone":
|
||||||
|
self.textEdit.setText("Person_1_Win")
|
||||||
|
|
||||||
|
if self.person_2=="scissors" and self.person_1 =="cloth":
|
||||||
|
self.textEdit.setText("Person_2_Win")
|
||||||
|
if self.person_2=="stone" and self.person_1 =="scissors":
|
||||||
|
self.textEdit.setText("Person_2_Win")
|
||||||
|
if self.person_2=="cloth" and self.person_1 =="stone":
|
||||||
|
self.textEdit.setText("Person_2_Win")
|
||||||
|
|
||||||
|
if self.person_1 ==self.person_2:
|
||||||
|
self.textEdit.setText("NO_WIN")
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
|
win = MainWindow()
|
||||||
|
win.show()
|
||||||
|
sys.exit(app.exec_())
|
49
Thread/code/test_Qthread_0716_ui.py
Normal file
49
Thread/code/test_Qthread_0716_ui.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Form implementation generated from reading ui file 'test_Qthread_0716_ui.ui'
|
||||||
|
#
|
||||||
|
# Created by: PyQt5 UI code generator 5.15.4
|
||||||
|
#
|
||||||
|
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
|
||||||
|
# run again. Do not edit this file unless you know what you are doing.
|
||||||
|
|
||||||
|
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
|
||||||
|
|
||||||
|
class Ui_MainWindow(object):
|
||||||
|
def setupUi(self, MainWindow):
|
||||||
|
MainWindow.setObjectName("MainWindow")
|
||||||
|
MainWindow.resize(676, 465)
|
||||||
|
self.centralwidget = QtWidgets.QWidget(MainWindow)
|
||||||
|
self.centralwidget.setObjectName("centralwidget")
|
||||||
|
self.test_bt = QtWidgets.QPushButton(self.centralwidget)
|
||||||
|
self.test_bt.setGeometry(QtCore.QRect(10, 50, 81, 31))
|
||||||
|
self.test_bt.setObjectName("test_bt")
|
||||||
|
self.view_1 = QtWidgets.QLabel(self.centralwidget)
|
||||||
|
self.view_1.setGeometry(QtCore.QRect(100, 50, 251, 231))
|
||||||
|
self.view_1.setText("")
|
||||||
|
self.view_1.setObjectName("view_1")
|
||||||
|
self.view_2 = QtWidgets.QLabel(self.centralwidget)
|
||||||
|
self.view_2.setGeometry(QtCore.QRect(360, 50, 251, 231))
|
||||||
|
self.view_2.setText("")
|
||||||
|
self.view_2.setObjectName("view_2")
|
||||||
|
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
|
||||||
|
self.textEdit.setGeometry(QtCore.QRect(360, 330, 261, 71))
|
||||||
|
self.textEdit.setObjectName("textEdit")
|
||||||
|
self.view_time = QtWidgets.QLabel(self.centralwidget)
|
||||||
|
self.view_time.setGeometry(QtCore.QRect(10, 10, 251, 31))
|
||||||
|
self.view_time.setText("")
|
||||||
|
self.view_time.setObjectName("view_time")
|
||||||
|
MainWindow.setCentralWidget(self.centralwidget)
|
||||||
|
self.statusbar = QtWidgets.QStatusBar(MainWindow)
|
||||||
|
self.statusbar.setObjectName("statusbar")
|
||||||
|
MainWindow.setStatusBar(self.statusbar)
|
||||||
|
|
||||||
|
self.retranslateUi(MainWindow)
|
||||||
|
QtCore.QMetaObject.connectSlotsByName(MainWindow)
|
||||||
|
|
||||||
|
def retranslateUi(self, MainWindow):
|
||||||
|
_translate = QtCore.QCoreApplication.translate
|
||||||
|
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
|
||||||
|
self.test_bt.setText(_translate("MainWindow", "PushButton"))
|
84
Thread/code/test_Qthread_0716_ui.ui
Normal file
84
Thread/code/test_Qthread_0716_ui.ui
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MainWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MainWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>676</width>
|
||||||
|
<height>465</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget">
|
||||||
|
<widget class="QPushButton" name="test_bt">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>50</y>
|
||||||
|
<width>81</width>
|
||||||
|
<height>31</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>PushButton</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QLabel" name="view_1">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>100</x>
|
||||||
|
<y>50</y>
|
||||||
|
<width>251</width>
|
||||||
|
<height>231</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QLabel" name="view_2">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>360</x>
|
||||||
|
<y>50</y>
|
||||||
|
<width>251</width>
|
||||||
|
<height>231</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QTextEdit" name="textEdit">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>360</x>
|
||||||
|
<y>330</y>
|
||||||
|
<width>261</width>
|
||||||
|
<height>71</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QLabel" name="view_time">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>10</x>
|
||||||
|
<y>10</y>
|
||||||
|
<width>251</width>
|
||||||
|
<height>31</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
88
Thread/code/test_process_0719.py
Normal file
88
Thread/code/test_process_0719.py
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
import multiprocessing as mp
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------範例4------------------------------
|
||||||
|
# A報數
|
||||||
|
def A_Count_off():
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'A : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
# B報數
|
||||||
|
def B_Count_off():
|
||||||
|
for i in range(5,10):
|
||||||
|
print(f'B : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
#
|
||||||
|
# if __name__=='__main__':
|
||||||
|
# process_list=[A_Count_off,B_Count_off]
|
||||||
|
# process_name =[]
|
||||||
|
# for i in range(0,len(process_list)):
|
||||||
|
# process_name.append(mp.Process(target=process_list[i]))
|
||||||
|
#
|
||||||
|
# for i in range(0,len(process_name)):
|
||||||
|
# process_name[i].start()
|
||||||
|
# # process_name[i].join()
|
||||||
|
#
|
||||||
|
# for i in range(0,len(process_name)):
|
||||||
|
# process_name[i].join()
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------範例5------------------------------
|
||||||
|
|
||||||
|
def Count_off(code_name):
|
||||||
|
print('process {} '.format(os.getpid()))
|
||||||
|
print('thread {} '.format(threading.current_thread().name))
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'{code_name} : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
# if __name__=='__main__':
|
||||||
|
# process_name_list=["A","B","C","D"]
|
||||||
|
# process_list=[]
|
||||||
|
# for i in range(0,len(process_name_list)):
|
||||||
|
# process_list.append(mp.Process(target=Count_off,args=process_name_list[i]))
|
||||||
|
# for i in range(0,len(process_list)):
|
||||||
|
# process_list[i].start()
|
||||||
|
# for i in range(0,len(process_list)):
|
||||||
|
# process_list[i].join()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------範例6-----------------------------
|
||||||
|
class Process_class(mp.Process):
|
||||||
|
def __init__(self, code_name):
|
||||||
|
mp.Process.__init__(self)
|
||||||
|
self.code_name = code_name
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
print('process {} '.format(os.getpid())) # 查看進程
|
||||||
|
print('thread {} '.format(threading.current_thread().name)) # 查看線程
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'{self.code_name} : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
def test_return(self):
|
||||||
|
return(f'{self.code_name}=END')
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
start_time = time.time()
|
||||||
|
process_name_list = ["A", "B", "C", "D"]
|
||||||
|
process_list = []
|
||||||
|
for i in range(0, len(process_name_list)):
|
||||||
|
process_list.append(Process_class(process_name_list[i]))
|
||||||
|
for i in range(0, len(process_list)):
|
||||||
|
process_list[i].start()
|
||||||
|
for i in range(0, len(process_list)):
|
||||||
|
process_list[i].join()
|
||||||
|
|
||||||
|
for i in range(0, len(process_list)):
|
||||||
|
print(process_list[i].test_return())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
107
Thread/code/test_thread_0715.py
Normal file
107
Thread/code/test_thread_0715.py
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
|
||||||
|
import threading
|
||||||
|
from queue import Queue #Thread 無法回傳值,所以要使用 Queue.put() 將要傳回的值存入 Queue,再用 Queue.get() 取出
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
|
# ----------------------------------------範例1------------------------------
|
||||||
|
# A報數
|
||||||
|
def A_Count_off():
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'A : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
# B報數
|
||||||
|
def B_Count_off():
|
||||||
|
for i in range(5,10):
|
||||||
|
print(f'B : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
thread_list=[A_Count_off,B_Count_off]
|
||||||
|
thread_num =[]
|
||||||
|
# for i in range(0,len(thread_list)):
|
||||||
|
# thread_num.append(threading.Thread(target=thread_list[i]))
|
||||||
|
#
|
||||||
|
# for i in range(0,len(thread_num)):
|
||||||
|
# thread_num[i].start()
|
||||||
|
# # thread_num[i].join()
|
||||||
|
#
|
||||||
|
# for i in range(0,len(thread_num)):
|
||||||
|
# thread_num[i].join()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------範例2------------------------------
|
||||||
|
|
||||||
|
def Count_off(code_name):
|
||||||
|
print('process {} '.format(os.getpid()))
|
||||||
|
print('thread {} '.format(threading.current_thread().name))
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'{code_name} : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
thread_name_list=["A","B","C","D"]
|
||||||
|
|
||||||
|
thread_list=[]
|
||||||
|
# for i in range(0,len(thread_name_list)):
|
||||||
|
# thread_list.append(threading.Thread(target=Count_off,args=thread_name_list[i]))
|
||||||
|
# for i in range(0,len(thread_list)):
|
||||||
|
# thread_list[i].start()
|
||||||
|
# for i in range(0,len(thread_list)):
|
||||||
|
# thread_list[i].join()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------範例3------------------------------
|
||||||
|
|
||||||
|
class Thread_class(threading.Thread):
|
||||||
|
def __init__(self,code_name):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.code_name = code_name
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
print('process {} '.format(os.getpid())) # 查看進程
|
||||||
|
print('thread {} '.format(threading.current_thread().name)) # 查看線程
|
||||||
|
for i in range(0,5):
|
||||||
|
print(f'{self.code_name} : {i}')
|
||||||
|
time.sleep(0.5)
|
||||||
|
def test_return(self):
|
||||||
|
return(f'{self.code_name}=END')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
thread_name_list=["A","B","C","D"]
|
||||||
|
thread_list=[]
|
||||||
|
for i in range(0,len(thread_name_list)):
|
||||||
|
thread_list.append(Thread_class(thread_name_list[i]))
|
||||||
|
for i in range(0,len(thread_list)):
|
||||||
|
thread_list[i].start()
|
||||||
|
for i in range(0,len(thread_list)):
|
||||||
|
thread_list[i].join()
|
||||||
|
|
||||||
|
for i in range(0,len(thread_list)):
|
||||||
|
print(thread_list[i].test_return())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
pyqt5/CODE/Pyqt5/__pycache__/test_pyqt5_0712_ui.cpython-39.pyc
Normal file
BIN
pyqt5/CODE/Pyqt5/__pycache__/test_pyqt5_0712_ui.cpython-39.pyc
Normal file
Binary file not shown.
BIN
pyqt5/CODE/Pyqt5/stop.jpg
Normal file
BIN
pyqt5/CODE/Pyqt5/stop.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
76
pyqt5/CODE/Pyqt5/test_pyqt5_0712.py
Normal file
76
pyqt5/CODE/Pyqt5/test_pyqt5_0712.py
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
import cv2
|
||||||
|
import sys, time, os
|
||||||
|
import numpy as np
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
from test_pyqt5_0712_ui import Ui_MainWindow
|
||||||
|
from PyQt5.QtCore import QDate,QTime
|
||||||
|
from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel,QMainWindow, QWidget, QPushButton
|
||||||
|
from PyQt5.QtCore import QThread, pyqtSignal
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 轉Qlabel的格式
|
||||||
|
def label_to_view(img): # 原圖
|
||||||
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
|
||||||
|
Ny, Nx, _ = img.shape
|
||||||
|
img = QtGui.QImage(img.data, Nx, Ny, Nx * 3, QtGui.QImage.Format_RGB888) # 須改格式
|
||||||
|
return img
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||||
|
def __init__(self, parent=None): #按鍵設定
|
||||||
|
super(MainWindow, self).__init__(parent)
|
||||||
|
self.setupUi(self)
|
||||||
|
|
||||||
|
self.view_1.setScaledContents(True); #設定view_1 自適應大小
|
||||||
|
|
||||||
|
self.open_file_bt.clicked.connect(self.open_file_clicked) # 按鍵open
|
||||||
|
self.horizontalSlider.valueChanged.connect(self.sliderValue) # 滑桿調整
|
||||||
|
|
||||||
|
self.img_check=False # 確認有無圖片
|
||||||
|
|
||||||
|
# 選擇圖片
|
||||||
|
def open_file_clicked(self):
|
||||||
|
file_filter = "Image Files (*.jpg *.jpeg *.png *.bmp *.mp4 *.avi);" # 規定格式
|
||||||
|
file_path, _ = QFileDialog.getOpenFileName(self, 'Open Image','',file_filter)
|
||||||
|
# 若沒圖 直接return 不執行下面程式
|
||||||
|
if file_path =="":
|
||||||
|
return
|
||||||
|
if file_path:
|
||||||
|
self.img_check = True
|
||||||
|
self.img = cv2.imread(file_path)
|
||||||
|
img = self.img.copy()
|
||||||
|
img = label_to_view(img)
|
||||||
|
self.view_1.setPixmap(QtGui.QPixmap.fromImage(img))
|
||||||
|
# 滑桿控制
|
||||||
|
def sliderValue(self):
|
||||||
|
# 若沒圖 直接return 不執行下面程式
|
||||||
|
if self.img_check==False:
|
||||||
|
return
|
||||||
|
|
||||||
|
binary_value = int(self.horizontalSlider.value()) # 二值化參數
|
||||||
|
self.horizontalSlider_text.setText(str(binary_value)) # 顯示數值
|
||||||
|
img = self.img.copy()
|
||||||
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
|
if binary_value < 2:
|
||||||
|
return
|
||||||
|
if (binary_value % 2 == 1):
|
||||||
|
img1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, binary_value, 2) # 動態二值
|
||||||
|
else:
|
||||||
|
img1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, binary_value + 1, 2) # 動態二值
|
||||||
|
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
|
||||||
|
img = label_to_view(img1)
|
||||||
|
self.view_1.setPixmap(QtGui.QPixmap.fromImage(img))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
|
win = MainWindow()
|
||||||
|
win.show()
|
||||||
|
sys.exit(app.exec_())
|
64
pyqt5/CODE/Pyqt5/test_pyqt5_0712_ui.py
Normal file
64
pyqt5/CODE/Pyqt5/test_pyqt5_0712_ui.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Form implementation generated from reading ui file 'test_pyqt5_0712_ui.ui'
|
||||||
|
#
|
||||||
|
# Created by: PyQt5 UI code generator 5.15.4
|
||||||
|
#
|
||||||
|
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
|
||||||
|
# run again. Do not edit this file unless you know what you are doing.
|
||||||
|
|
||||||
|
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
|
||||||
|
|
||||||
|
class Ui_MainWindow(object):
|
||||||
|
def setupUi(self, MainWindow):
|
||||||
|
MainWindow.setObjectName("MainWindow")
|
||||||
|
MainWindow.resize(1100, 864)
|
||||||
|
self.centralwidget = QtWidgets.QWidget(MainWindow)
|
||||||
|
self.centralwidget.setObjectName("centralwidget")
|
||||||
|
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
|
||||||
|
self.gridLayout.setObjectName("gridLayout")
|
||||||
|
self.open_file_bt = QtWidgets.QPushButton(self.centralwidget)
|
||||||
|
self.open_file_bt.setMinimumSize(QtCore.QSize(91, 51))
|
||||||
|
self.open_file_bt.setMaximumSize(QtCore.QSize(131, 81))
|
||||||
|
self.open_file_bt.setObjectName("open_file_bt")
|
||||||
|
self.gridLayout.addWidget(self.open_file_bt, 0, 0, 1, 1)
|
||||||
|
self.view_1 = QtWidgets.QLabel(self.centralwidget)
|
||||||
|
self.view_1.setMinimumSize(QtCore.QSize(641, 491))
|
||||||
|
self.view_1.setObjectName("view_1")
|
||||||
|
self.gridLayout.addWidget(self.view_1, 0, 1, 2, 3)
|
||||||
|
spacerItem = QtWidgets.QSpacerItem(20, 736, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
|
||||||
|
self.gridLayout.addItem(spacerItem, 1, 0, 2, 1)
|
||||||
|
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
|
||||||
|
self.horizontalSlider.setMinimumSize(QtCore.QSize(301, 22))
|
||||||
|
self.horizontalSlider.setMinimum(1)
|
||||||
|
self.horizontalSlider.setMaximum(255)
|
||||||
|
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
|
||||||
|
self.horizontalSlider.setObjectName("horizontalSlider")
|
||||||
|
self.gridLayout.addWidget(self.horizontalSlider, 2, 1, 1, 1)
|
||||||
|
self.horizontalSlider_text = QtWidgets.QTextEdit(self.centralwidget)
|
||||||
|
self.horizontalSlider_text.setMinimumSize(QtCore.QSize(156, 71))
|
||||||
|
self.horizontalSlider_text.setMaximumSize(QtCore.QSize(156, 71))
|
||||||
|
font = QtGui.QFont()
|
||||||
|
font.setFamily("Agency FB")
|
||||||
|
font.setPointSize(24)
|
||||||
|
self.horizontalSlider_text.setFont(font)
|
||||||
|
self.horizontalSlider_text.setLayoutDirection(QtCore.Qt.LeftToRight)
|
||||||
|
self.horizontalSlider_text.setObjectName("horizontalSlider_text")
|
||||||
|
self.gridLayout.addWidget(self.horizontalSlider_text, 2, 2, 1, 1)
|
||||||
|
spacerItem1 = QtWidgets.QSpacerItem(405, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
|
||||||
|
self.gridLayout.addItem(spacerItem1, 2, 3, 1, 1)
|
||||||
|
MainWindow.setCentralWidget(self.centralwidget)
|
||||||
|
self.statusbar = QtWidgets.QStatusBar(MainWindow)
|
||||||
|
self.statusbar.setObjectName("statusbar")
|
||||||
|
MainWindow.setStatusBar(self.statusbar)
|
||||||
|
|
||||||
|
self.retranslateUi(MainWindow)
|
||||||
|
QtCore.QMetaObject.connectSlotsByName(MainWindow)
|
||||||
|
|
||||||
|
def retranslateUi(self, MainWindow):
|
||||||
|
_translate = QtCore.QCoreApplication.translate
|
||||||
|
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
|
||||||
|
self.open_file_bt.setText(_translate("MainWindow", "選擇圖片"))
|
||||||
|
self.view_1.setText(_translate("MainWindow", "TextLabel"))
|
126
pyqt5/CODE/Pyqt5/test_pyqt5_0712_ui.ui
Normal file
126
pyqt5/CODE/Pyqt5/test_pyqt5_0712_ui.ui
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MainWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MainWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1100</width>
|
||||||
|
<height>864</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget">
|
||||||
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QPushButton" name="open_file_bt">
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>91</width>
|
||||||
|
<height>51</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="maximumSize">
|
||||||
|
<size>
|
||||||
|
<width>131</width>
|
||||||
|
<height>81</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>選擇圖片</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="1" rowspan="2" colspan="3">
|
||||||
|
<widget class="QLabel" name="view_1">
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>641</width>
|
||||||
|
<height>491</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>TextLabel</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" rowspan="2">
|
||||||
|
<spacer name="verticalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Vertical</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>20</width>
|
||||||
|
<height>736</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="1">
|
||||||
|
<widget class="QSlider" name="horizontalSlider">
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>301</width>
|
||||||
|
<height>22</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="minimum">
|
||||||
|
<number>1</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>255</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="2">
|
||||||
|
<widget class="QTextEdit" name="horizontalSlider_text">
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>156</width>
|
||||||
|
<height>71</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="maximumSize">
|
||||||
|
<size>
|
||||||
|
<width>156</width>
|
||||||
|
<height>71</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
<property name="font">
|
||||||
|
<font>
|
||||||
|
<family>Agency FB</family>
|
||||||
|
<pointsize>24</pointsize>
|
||||||
|
</font>
|
||||||
|
</property>
|
||||||
|
<property name="layoutDirection">
|
||||||
|
<enum>Qt::LeftToRight</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="3">
|
||||||
|
<spacer name="horizontalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>405</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
Loading…
Reference in New Issue
Block a user