vault backup: 2022-09-28 19:13:41
Affected files: .obsidian/workspace 03. Programming/COM/20210726 - COM Interface.md 03. Programming/DB/MySQL.md 03. Programming/DB/sqlite.md 03. Programming/Design Pattern.md 03. Programming/FFMPEG/00. Introduction.md 03. Programming/FFMPEG/01. Setup.md 03. Programming/FFMPEG/FFMpeg.md 03. Programming/Flask.md 03. Programming/Media Foundation/20210604 - Windows media foundation.md 03. Programming/OpenCV.md 03. Programming/OpenGL.md 03. Programming/Python/argparse.ArgumentParser.md 03. Programming/Python/decorator.md 03. Programming/Python/logging.md 03. Programming/Python/opencv.md 03. Programming/Python/subprocess.md 03. Programming/Python/threading.md 03. Programming/Python/tkinter.md 03. Programming/Python/檢測工具.md 03. Programming/QT/Dropdown button.md 03. Programming/QT/QVariant.md 03. Programming/QT/Qt.md 03. Programming/UML.md 03. Programming/演算法.md 04. 資料收集/99. templates/blogHeader.md 04. 資料收集/99. templates/date.md 04. 資料收集/99. templates/front matter.md 04. 資料收集/99. templates/note.md 04. 資料收集/99. templates/table.md 04. 資料收集/99. templates/thisWeek.md 04. 資料收集/99. templates/日記.md 04. 資料收集/99. templates/讀書筆記.md 04. 資料收集/Linux/CLI/cut.md 04. 資料收集/Linux/CLI/scp.md 04. 資料收集/Linux/CLI/timedatectl.md 04. 資料收集/Linux/Programming.md 04. 資料收集/Linux/Ubuntu.md 04. 資料收集/Tool Setup/Hardware/RaspberryPi.md 04. 資料收集/Tool Setup/Software/Chrome.md 04. 資料收集/Tool Setup/Software/Obisidian.md 04. 資料收集/Tool Setup/Software/SublimeText.md 04. 資料收集/Tool Setup/Software/VirtualBox.md 04. 資料收集/Tool Setup/Software/Visual Studio Code.md 04. 資料收集/Tool Setup/Software/Windows Setup.md 04. 資料收集/Tool Setup/Software/Windows Terminal.md 04. 資料收集/Tool Setup/Software/freefilesync.md 04. 資料收集/Tool Setup/Software/vim.md 04. 資料收集/名言佳句.md 04. 資料收集/架站/Gitea.md 04. 資料收集/架站/HTTP Server/Apache.md 04. 資料收集/架站/HTTP Server/Nginx/Reverse Proxy(Layer4).md 04. 資料收集/架站/Pelican blog.md 04. 資料收集/架站/Proxmox VE.md 04. 資料收集/架站/SWAG Reverse proxy.md 04. 資料收集/架站/Storj.md 04. 資料收集/架站/Trojan.md 04. 資料收集/每週外食.md 04. 資料收集/科技/802.11.md 04. 資料收集/科技/HDR Sensor.md 04. 資料收集/科技/量子電腦.md 04. 資料收集/科技/鋰電池.md 04. 資料收集/興趣嗜好/RC/Traxxas Sledge.md 04. 資料收集/興趣嗜好/RC/好盈電變調整中立點.md 04. 資料收集/興趣嗜好/RC/差速器調教教學.md 04. 資料收集/興趣嗜好/模型/舊化作例.md 04. 資料收集/興趣嗜好/軍武/虎式.md 04. 資料收集/讀書筆記/20201201 - 學習如何學習.md 04. 資料收集/讀書筆記/20201218 - Kotlin權威2.0.md 04. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md 04. 資料收集/讀書筆記/20210119 - 中產悲歌.md 04. 資料收集/讀書筆記/20210220 - 最高學習法.md 04. 資料收集/讀書筆記/20210320 - 最高學以致用法.md 04. 資料收集/讀書筆記/20210406 - 精準購買.md 04. 資料收集/讀書筆記/20210723 - 高手學習.md 04. 資料收集/讀書筆記/20220526 - 深入淺出設計模式.md 04. 資料收集/讀書筆記/20220619 - 精確的力量.md 04. 資料收集/軟體工具/IPFS.md 04. 資料收集/軟體工具/MkDocs.md 04. 資料收集/軟體工具/Obsidian.md 04. 資料收集/軟體工具/docker.md 04. 資料收集/軟體工具/git/apply.md 04. 資料收集/軟體工具/git/submodule.md 04. 資料收集/軟體工具/youtube-dl.md 04. 資料收集/面試準備/技术面试最后反问面试官的话.md
This commit is contained in:
96
03. Programming/Python/tkinter.md
Normal file
96
03. Programming/Python/tkinter.md
Normal file
@@ -0,0 +1,96 @@
|
||||
### 把matplotlib包裝成獨立視窗
|
||||
```python
|
||||
class Plot2D(Frame):
|
||||
def __init__(self, parent, dataCollector, **kwargs):
|
||||
Frame.__init__(self, parent.mainWindow, **kwargs)
|
||||
|
||||
self.parent = parent
|
||||
self.mainWindows = Toplevel(parent.mainWindow)
|
||||
self.mainWindows.title("AF State")
|
||||
self.figure = plt.Figure(figsize=(9,5), dpi=100)
|
||||
self.figure.suptitle('AF value plot', fontsize=16)
|
||||
self.ax = self.figure.add_subplot(111)
|
||||
self.canvas = FigureCanvasTkAgg(self.figure, master=self.mainWindows)
|
||||
self.canvas.get_tk_widget().pack(fill='both')
|
||||
self.axline = None
|
||||
|
||||
self.dataCollector = dataCollector
|
||||
self.dataCollector.start()
|
||||
|
||||
def close(self):
|
||||
print("Plot2D close")
|
||||
self.mainWindows.destroy()
|
||||
self.dataCollector.stop()
|
||||
self.dataCollector = None
|
||||
|
||||
def draw(self):
|
||||
if self.dataCollector:
|
||||
datax, datay = self.dataCollector.getPlotData()
|
||||
self.ax.clear()
|
||||
self.ax.set_xlabel('Last {} datas'.format(self.dataCollector.getDataLength()))
|
||||
self.axline, = self.ax.plot(datax, datay)
|
||||
self.canvas.draw()
|
||||
|
||||
def getWindow(self):
|
||||
return self.mainWindows
|
||||
|
||||
def getLastData(self):
|
||||
return self.dataCollector.getLastData()
|
||||
```
|
||||
|
||||
其中這一行:
|
||||
```python
|
||||
self.mainWindows = Toplevel(parent.mainWindow)
|
||||
```
|
||||
是用來開一個新的視窗,其中的`parent.mainWindow`就是用`tk.TK()`所產生出來的root。
|
||||
|
||||
因為需要一直更新資料,所以需要的一個`DataCollector`來提供資料,`DataCollector`會提供畫圖需要的list,如:
|
||||
```python
|
||||
datax, datay = self.dataCollector.getPlotData()
|
||||
```
|
||||
|
||||
`DataCollector`的定義如下:
|
||||
```python
|
||||
class AfStateCollector(threading.Thread):
|
||||
def __init__(self, dataLength=100, pollingInterval=0.033):
|
||||
threading.Thread.__init__(self)
|
||||
self.dataLength = dataLength
|
||||
self.pollingInterval = pollingInterval
|
||||
self.stopEvent = threading.Event()
|
||||
self.data = []
|
||||
self.xdata = []
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
if self.stopEvent.is_set():
|
||||
break
|
||||
|
||||
afValue = self.readAf()
|
||||
self.data.append(afValue)
|
||||
self.xdata.append(len(self.xdata))
|
||||
if len(self.data) > self.dataLength:
|
||||
self.data = self.data[-self.dataLength:]
|
||||
self.xdata = list(range(self.dataLength))
|
||||
|
||||
# print(f'afValue = {afValue}')
|
||||
time.sleep(self.pollingInterval)
|
||||
|
||||
print("AfStateCollector stopped.")
|
||||
|
||||
def readAf(self):
|
||||
ReadTestXUreg_cmd = "lvreg testxu read 10"
|
||||
ReadTestXUreg_cmd_process = subprocess.Popen(ReadTestXUreg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
outstring, err = ReadTestXUreg_cmd_process.communicate()
|
||||
outstring = outstring.strip().decode('utf-8')
|
||||
outstring = int(outstring, 16)
|
||||
outstring_H = (outstring & 0xFF00) / 256
|
||||
outstring_L = outstring & 0xFF
|
||||
outAFStat = int(outstring_L * 256 + outstring_H)
|
||||
|
||||
return outAFStat
|
||||
```
|
||||
|
||||
- [Python GUI之tkinter視窗視窗教程大集合(看這篇就夠了) - IT閱讀](https://www.itread01.com/content/1547705544.html)
|
||||
- [【Python】改善 VideoCapture 的影像延遲 | 夏恩的程式筆記 - 點部落](https://dotblogs.com.tw/shaynling/2017/12/28/091936)
|
||||
- [Displaying a video feed with OpenCV and Tkinter - PyImageSearch](https://www.pyimagesearch.com/2016/05/30/displaying-a-video-feed-with-opencv-and-tkinter/)
|
||||
Reference in New Issue
Block a user