Ich bastle gerade - und das sollte man ja bekanntlich nicht
So richtig tolle ganzheitliche Beispiele findet man nicht zum genannten Thema (zumindest ich nicht) für Hinweise wäre ich sehr froh.
Den Code unten habe ich zum grossen Teil gefunden und dann versucht für mich zu ergänzen. Das mit der Übergabe der Queue's muss doch anders gehn - oder?
Zur Info: Dort wo die Counter Upgedatet werden, soll später der Code hin, welcher Daten von der Seriellen Schnittstelle liest.
Wäre toll, wenn jemand den Code mal ansieht und vorallem Böck und Bastelleien aufzeigt. Oder ob der so im grossen und ganzen "gut" ist. Perfekt muss er nicht sein. Man findet immer was - es geht mir ums Grundprinzip.
Wie schon erwähnt: Mich würde auch grössere Projekte interessieren vorallem mit GUI wie die Aussehen, Struktur usw. Wenn ihr welche kennt - gerne. Allenfalls auch als Datei(en) über PN.
Danke schon mal.
Code: Alles auswählen
import tkinter as tk
from time import sleep
from threading import Thread
from queue import Queue
from serial import Serial
class GuiPart:
def __init__(self, master, queue, endCommand):
self.queue = queue
# Set up the GUI
self.lbl1 = tk.Label(master, text='Counter 1')
self.lbl1.pack()
self.lbl2 = tk.Label(master, text='Value 1')
self.lbl2.pack()
self.lbl3 = tk.Label(master, text='Counter 2')
self.lbl3.pack()
self.lbl4 = tk.Label(master, text='Value 2')
self.lbl4.pack()
self.lbl5 = tk.Label(master, text='Counter 3')
self.lbl5.pack()
self.lbl6 = tk.Label(master, text='Value 3')
self.lbl6.pack()
self.btn_close = tk.Button(master, text='Close', command=endCommand)
self.btn_close.pack()
def processIncoming(self):
"""Handle all messages currently in the queue, if any."""
while self.queue.qsize():
try:
# Check contents of message and do whatever is needed. As a
# simple test, print it (in real life, you would
# suitably update the GUI's display in a richer fashion).
msg1 = self.queue.get(0)
msg2 = self.queue.get(0)
msg3 = self.queue.get(0)
self.lbl2.config(text= msg1)
self.lbl2.update()
self.lbl4.config(text= msg2)
self.lbl4.update()
self.lbl6.config(text= msg3)
self.lbl6.update()
except queue.Empty:
# just on general principles, although we don't
# expect this branch to be taken in this case
pass
class ThreadedClient:
"""
Launch the main part of the GUI and the worker thread. periodicCall and
endApplication could reside in the GUI part, but putting them here
means that you have all the thread controls in a single place.
"""
def __init__(self, master):
"""
Start the GUI and the asynchronous threads. We are in the main
(original) thread of the application, which will later be used by
the GUI as well. We spawn a new thread for the worker (I/O).
"""
self.counter1 = 100
self.counter2 = 200
self.counter3 = 300
self.master = master
# Create the queue
self.queue = Queue()
# Set up the GUI part
self.gui = GuiPart(master, self.queue, self.endApplication)
# Set up the thread to do asynchronous I/O
# More threads can also be created and used, if necessary
self.running = 1
self.thread1 = Thread(target=self.workerThread1)
self.thread1.start()
# Start the periodic call in the GUI to check if the queue contains
# anything
self.periodicCall()
def periodicCall(self):
"""
Check every 200 ms if there is something new in the queue.
"""
self.gui.processIncoming()
if not self.running:
# This is the brutal stop of the system. You may want to do
# some cleanup before actually shutting it down.
import sys
sys.exit(1)
self.master.after(200, self.periodicCall)
def workerThread1(self):
"""
This is where we handle the asynchronous I/O. For example, it may be
a 'select( )'. One important thing to remember is that the thread has
to yield control pretty regularly, by select or otherwise.
"""
while self.running:
# To simulate asynchronous I/O, we create a random number at
# random intervals. Replace the following two lines with the real
# thing.
sleep(1)
self.counter1 += 1
self.counter2 += 1
self.counter3 += 1
self.queue.put(self.counter1)
self.queue.put(self.counter2)
self.queue.put(self.counter3)
def endApplication(self):
self.running = 0
root = tk.Tk()
root.geometry('170x200+30+30')
client = ThreadedClient(root)
root.mainloop()