因此,我基本上想将不同的值传递给我的Signal Generator,而不是在我的代码中使用默认的Frequency/Amplitude值。

我尝试了get小部件的Entry方法,但是没有运气:

我认为它应该像这样:

inst.write(f"SOUR:POW:LEV:IMM:AMPL {ampl_input.get()}")


但是由于它以字符串形式返回文本,所以我应该以某种方式将该字符串传递给按钮。

import visa
import tkinter as tk


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

ampl = str(-20)
freq_input = str(input())



inst.write("OUTP ON")

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

def amplset_smb100a():
    inst.write("SOUR:POW:LEV:IMM:AMPL " + ampl) #TheSOUR:FREQ etc are commands for the instrument.



HEIGHT = 400
WIDTH = 600

root = tk.Tk()

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)

button = tk.Button(frame, text="Set Ampl", font=40, command=amplset_smb100a)
button.place(relx=0.01, relheight=1, relwidth=0.3)

button = tk.Button(frame, text="IDN", font=40, command=show_idn)
button.place(relx=0.35, relheight=1, relwidth=0.3)

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

lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.8, relwidth=0.7, relheight=0.2, anchor='n')

label = tk.Label(lower_frame)
label.place(relwidth=1, relheight=1)

root.mainloop()

最佳答案

def freqset_smb100a():
    print(inst.write("SOUR:FREQ:CW 150000000"))
    inst.write(f"SOUR:POW:LEV:IMM:AMPL {ampl_input.get()}")
    print(inst.write("SOUR:POW:LEV:IMM:AMPL -25"))
    print(inst.write("OUTP ON"))


也许是这样的吗?

关于python - 是否可以使用Tkinter条目作为输入,通过按钮触发器将值传递给信号发生器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56699110/

10-12 22:22