[Tkinter-discuss] How to display the values of dictionary to tk window.

Srinivas Rao srinivas.rambha at gmail.com
Mon Aug 19 12:14:22 CEST 2013


This code is to receive and display the text on the tk window.But what
i want to achieve here is: When the character is sent from master to
slave. Receive the character on the slave, look up the character in
the dict, display the value that corresponds to the key. I tried but
couldn't succeed, can any one help me with the correct code.

for example: when slave received a key called 'hw' from master,It has
to display "hello world" on slave pc , when '0' received from master,
should display "how are you?" on slave pc. thanks in advance.


import serialimport threadingimport Queueimport Tkinter as tkfrom
Tkinter import *import timeimport sys
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")
        self.time = ''
        self.clock = tk.Label(self, font=('times', 50, 'bold'),
fg='black',     bg='yellow')
        self.clock.pack(side='bottom', anchor='se')

        frameLabel = tk.Frame(self, padx=10, pady =50)
        self.text = tk.Text(frameLabel, wrap='word',
bg=self.cget('bg'), relief='flat')
        frameLabel.pack()
        self.text.pack()
        self.queue = Queue.Queue()
        thread = SerialThread(self.queue)
        thread.start()
        self.process_serial()
self.msgs = {
        'hw':"hello world",
        'wpp':"welcome to python programming",
        'h':"hello",
        0:"how are you?",
        1:"bye....",
        'egm': "english general message how are you",
        'egm2':"If the world is flat be carefull not to fall off"
        }

    def process_serial(self):
        self.time = time.strftime('%H:%M:%S')
        self.clock.config(text=self.time)
        firstitem = True
        while self.queue.qsize():
            try:
                new = self.queue.get()
                size = sys.getsizeof(new)
                if size<40:
                    self.text.config(font='TimesNewRoman 100')
                elif size>=40 and size<70:
                    self.text.config(font='TimesNewRoman 75')
                else:
                     self.text.config(font='TimesNewRoman 50')
                if firstitem:
                    self.text.delete(1.0, 'end')
                firstitem = False
                self.text.tag_configure("tag-center", justify='center')
                #self.text.tag_add("center", 1.0, "end")
                self.text.insert('end',
my_dictionary.setdefault(signal, 'wrong signal'))
                #self.text.insert('end', new, 'tag-center')
            except Queue.Empty:
                pass
        self.after(1000, self.process_serial)

app = App()
app.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20130819/7118f70f/attachment-0001.html>


More information about the Tkinter-discuss mailing list