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

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

感谢上一个问题的帮助,我现在可以使用以下代码。我可以打印我对 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()

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

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

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

变化:

print (instr)

收件人:

self.label3.config(text = instr)