如何使用变量读回Tkinter文本框?

时间:2017-09-08 16:44:44

标签: python tkinter pyvisa

感谢上一个问题的帮助,我现在有以下代码可以使用。我可以打印我对shell的响应,但有没有办法将响应更新到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 个答案:

答案 0 :(得分:1)

您可以使用config()方法更新任何标签。

因此,您可以直接使用config(text=instr)

配置文本参数,而不是打印该值

变化:

print (instr)

要:

self.label3.config(text = instr)