pythonpython-3.xtkinterpyvisa

Is it possible to use Tkinter entry as input, with a button trigger, to pass values to a Signal Generator?


So I basically want to pass different values to my Signal Generator, instead of having a default Frequency/Amplitude value on my code.

I tried the get method of the Entry widget, with no luck:

I think it should look like this :

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

But since it returns the text as string, then I should somehow pass that string to the button.

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()

Solution

  • 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"))
    

    Maybe something like this?