类型错误:只能将 str(不是“设置”)连接到 str

Roz*_*kos 0 python tkinter python-3.x

基本上我正在尝试使用Entry box我在 tkinter 中制作的 as input,将 a 传递value给我的信号发生器。但是我收到标题中提到的错误。如果我通过value终端,它可以正常工作,所以这可能是 tkinter 的问题,而不是仪器(罗德和施瓦茨 SMB100A)的问题。

我尝试将值传递为string,正如错误所暗示的那样,但没有运气。

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()
Run Code Online (Sandbox Code Playgroud)

这是我按下button传递值时得到的错误。

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
Run Code Online (Sandbox Code Playgroud)

blu*_*ote 5

你的问题是,f"SOUR:FREQ:CW " + {str(input_var.get())}{str(...)}是一个集文字。它按设置就地创建,您正尝试将其添加到字符串中。

您使用的格式化字符串想要的只是类似于

 print(f"SOUR:FREQ:CW {input_var.get()}")
Run Code Online (Sandbox Code Playgroud)

也就是说,两者之间的任何内容都{}将被评估,转换为字符串,并插入到那里。

如果您的设备不支持较新的 python 版本,请删除f字符串前面的,然后将其设为

write("SOUR:FREQ:CW" + str(input_var.get()))
Run Code Online (Sandbox Code Playgroud)