Rozakos
Rozakos

Reputation: 618

TypeError: can only concatenate str (not "set") to str

Basically i am trying to use the Entry box i made in tkinter as input, to pass a value to my Signal Generator. But i get the error mentioned in the title. If i pass the value through the terminal though, it works without a problem, so it's probably an issue with tkinter, and not with the Instrument ( Rohde and Schwarz SMB100A).

I tried passing the value as string, as the error suggests, but no luck.

import visa
import tkinter as tk


rm = visa.ResourceManager()
print(rm.list_resources())
inst = rm.open_resource('TCPIP::192.168.100.200::INSTR')

#This one would work, after i bind the function to the button,
#i just press it and it passes the preset value. 
#All i want to do is pass a value through the Entry widget, 
#instead of having a set one.
#freq = str(250000) 
#def freqset_smb100a():
    #inst.write("SOUR:FREQ:CW " + freq)

inst.write("OUTP ON")

def freqset_smb100a():
    inst.write(f"SOUR:FREQ:CW " + {str(input_var.get())})



HEIGHT = 400
WIDTH = 600

root = tk.Tk()

input_var = tk.StringVar()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='#80c1ff', bd=5)
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')

button = tk.Button(frame, text="Set Freq", font=40, command=freqset_smb100a)
button.place(relx=0.7, relheight=1, relwidth=0.3)

entry = tk.Entry(frame, font=15, textvariable=str(input_var.get))
entry.place(relx=0.35, relheight=1, relwidth=0.3)


root.mainloop()

That's the error i get when i press the button to pass the value.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\vrozakos\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/PyTests/Signal_gen_v2.py", line 19, in freqset_smb100a
    inst.write(f"SOUR:FREQ:CW " + {str(input_var.get())})
TypeError: can only concatenate str (not "set") to str

Upvotes: 2

Views: 30141

Answers (1)

blue note
blue note

Reputation: 29071

You problem is that in f"SOUR:FREQ:CW " + {str(input_var.get())} the {str(...)} is a set literal. It creates as set, in-place, which you are trying to add to a string.

What you want with the formatted string you are using is simply something like

 print(f"SOUR:FREQ:CW {input_var.get()}")

That is, anything between {} will be evaluated, converted to string, and inserted there.

If your device does not support newer python versions, remove the f in front of the string, and just make it

write("SOUR:FREQ:CW" + str(input_var.get()))

Upvotes: 10

Related Questions