[Tkinter-discuss] python: Display the text on tk window.
Michael Lange
klappnase at web.de
Thu Jul 18 12:32:28 CEST 2013
Hi,
I forwarded this to the list (hope that's ok), I thought it might be of
interest for other people, too.
On Thu, 18 Jul 2013 13:07:41 +0530
Srinivas Rao <srinivas.rambha at gmail.com> wrote:
> Two pc's are connected with RS 232 label for serial communication. one
> pc for transmitting and other pc for receiving.
> I will run this code in receiving pc.
> This code is to receive text from a transmitting pc and display it on
> tkinter window of the receiving pc.
> The received text has to be displayed on tk window Until it receives
> other text from transmiting pc.
> But With my code the contents of the received text is displaying on tk
> window for only few milliseconds, then after tk window looks plain
> with no contents of data. When it receives another text from
> transmitter, it again displays the text only for milliseconds and the
> text vanishes.
>
> Here
> The text is not a pre defined bunch of messages of equal lengh, the
> length of the text vary all the time.
> The time period between the text's is not constant, it also vary all
> the time.
> The transmitter sends text at differnet time periods,first text in 5
> min, next text may be in 10 min and the third text may be in 30 min and
> so on. I don't want to clear the contents of the text from the tk
> window until it receives next text from transmitter.
>
> The font size i choose is 37 and I dont need a scrool bar to scrool the
> window, because transmitter sends maximum of 5 lines to display on
> receiver tk widow.
> import serial
> import threading
> import Queue
> import Tkinter as tk
>
>
> class SerialThread(threading.Thread):
> def __init__(self, queue):
> threading.Thread.__init__(self)
> self.queue = queue
> def run(self):
> s = serial.Serial('COM11',9600)
>
> while True:
> if s.inWaiting():
> text = s.readline(s.inWaiting())
> self.queue.put(text)
>
> class App(tk.Tk):
> def __init__(self):
> tk.Tk.__init__(self)
> self.geometry("1360x750")
> frameLabel = tk.Frame(self, padx=40, pady =40)
> self.text = tk.Text(frameLabel, wrap='word',
> font='TimesNewRoman 37', bg=self.cget('bg'), relief='flat')
> frameLabel.pack()
> self.text.pack()
> self.queue = Queue.Queue()
> thread = SerialThread(self.queue)
> thread.start()
> self.process_serial()
>
> def process_serial(self):
> self.text.delete(1.0, 'end')
> while self.queue.qsize():
> try:
> self.text.insert('end', self.queue.get())
> except Queue.Empty:
> pass
> self.after(1500, self.process_serial)
>
> app = App()
> app.mainloop()
Of course the problem here is the call to self.text.delete() in
process_serial() which forces the Text's contents to be deleted after
1500 ms in any case. What you probably intended is something like:
def process_serial(self):
while self.queue.qsize():
try:
new = self.queue.get()
self.text.delete(1.0, 'end')
self.text.insert('end', new)
except Queue.Empty:
pass
self.after(1500, self.process_serial)
which will empty the text only if new content is available (ok, this
does not handle the case of multiple new items in the queue, but I guess
you'll see the point...).
Regards
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
What kind of love is that? Not to be loved; never to have shown love.
-- Commissioner Nancy Hedford, "Metamorphosis",
stardate 3219.8
More information about the Tkinter-discuss
mailing list