Serial port routine works in terminal; not in TK

Post here if you need help with creating a Graphical User Interface in Python.

Serial port routine works in terminal; not in TK

Postby jimwims » Fri Apr 18, 2014 8:52 pm

I am trying to write a TKinter GUI script for controlling the operation of a radio via a serial port. The first step is to have it retrieve and display in a label the frequency the radio is currently tuned to. It simply sends the code which requests the frequency, i.e., "FEFE68E003FD'", to the radio, waits a short time for the response, parses out and decodes the frequency from the response and then sets the text variable for the label to that value. This is done once a second.

When I run the script in a terminal (using the commented out for statement at the end and), it seems to work fine. But when I run it with Tkinter, it typically works fine for the first iteration and on occasional iterations after that, but on the vast majority of iterations, the string received back from the radio is highly truncated. I typically get about 8 characters back rather than the expected 34.

I am doing this on a Win7 computer. A prior, virtually identical version on a Ubuntu machine worked well.

Does anybody have any suggestions about where I may be going wrong?

Thanks in advance.

Code: Select all
from Tkinter import *
from ttk import *
import serial
import string
import time
from datetime import datetime
import xmlrpclib

def readrig():          #retrievs rig parameters for display
   rig = serial.Serial('COM1')
   send = 'FEFE68E003FD'      #Retrieve Frequency
     received ='hex')
   receivedfreq = received[-6:-4] + received[-8:-6] + received[-10:-9] + '.' + received[-9:-8]
   print received
   print receivedfreq
   freqlabel.after(1000, readrig)  #this is what causes freq etc. to repeatedly update

root =  Tk()

freqframe = Frame(root)

freq = StringVar()
freqlabel = Label (freqframe, textvariable = freq, width = 7, anchor = CENTER, font = ("Helvetica", "50", "bold"))
freqlabel.pack(side = LEFT)

#for i in range(0, 9):

Last edited by stranac on Fri Apr 18, 2014 9:03 pm, edited 1 time in total.
Reason: First post lock.
Posts: 1
Joined: Fri Apr 18, 2014 8:00 pm

Re: Serial port routine works in terminal; not in TK

Postby micseydel » Mon Apr 21, 2014 11:28 pm

You mention that there seemed to be a difference between operating systems, so I wonder, were you running it in a terminal in Linux and in something like IDLE for Windows? Because running TK apps in IDLE often results in conflicts.
Join the #python-forum IRC channel on for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2028
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests