How to use a variable to read back into a Tkinter text box?

139 views Asked by At

Thanks to help from a previous question I now have the following code which works. I am able to print my response to the shell, but is there a way to update that response into label3?

    from tkinter import *

    instr = ""
    class Application():


        def __init__(self, master):

            self.master = master
            root.geometry("250x150+300+300")
            self.label1 = Label(master, text="Enter UUT Address")
            self.label1.pack()
            self.entry1 = Entry(self.master)
            self.entry1.pack()
            self.button1 = Button(self.master, text = "Query Instrument", width = 25, command = self.query_instrument)
            self.button1.pack()
            self.button2 = Button(self.master, text = 'Quit', width = 25, command = self.close_windows)
            self.button2.pack()
            self.label2 = Label(master, text= "Instrument Response")
            self.label2.pack()
            self.label3 = Label(master, text= instr)
            self.label3.pack()

     def close_windows(self):
            self.master.destroy()    

     def query_instrument(self):
            addr = self.entry1.get()
            import visa
            rm = visa.ResourceManager()
            rm.list_resources()
            ('ASRL1::INSTR', 'ASRL2::INSTR', 'GPIB0::' + str(addr) + '::INSTR')
             my_instrument = rm.open_resource('GPIB0::' + str(addr) + '::INSTR')
             global instr
             instr = my_instrument.query('*IDN?')
             print (instr)


    root = Tk()
    Application(root)
    root.mainloop()
1

There are 1 answers

2
Mike - SMT On BEST ANSWER

You can update any label by using the config() method.

So instead of printing the value you can configure the text argument directly with config(text=instr)

Change:

print (instr)

To:

self.label3.config(text = instr)