如何创建"Console"显示以显示我在 tkinter 窗口中运行的代码的输出?



所以我有点被困在这个问题上,我真的不知道从这里开始。我正在尝试创建一个tkinter gui来控制视差RFID读/写模块。到目前为止,我已经设法对 tkinter 窗口进行了编码,并创建了一个按钮,按下它时将开始读取 rfid 标签。我的问题是我不知道如何创建一个将更新的标签,以便标签的内存内容显示在 tkinter 窗口中,而不仅仅是控制台中。

这是python 2.7代码。

这是我用来将标签内容打印到控制台的代码。

    print ("Read App Starting...")
    print ("Reading tag's User Data...")
    for ix in range(3, 31):
      while True:
        ser.write('!RW' + chr(CMD_READ) + chr(ix))    # send command
        buf = ser.read(5)           # get bytes (will block until received)
        if buf[0] == chr(ERR_OK):   # if valid data received with no error, continue
          break
      print ("%2d") % ix, ": ",
      for iy in range(1,len(buf)):  # display data
        sys.stdout.write("%02X" % ord(buf[iy]))
      print("")

我已将其保存到一个文件中,并将其导入到我的 tkinter 代码中。

金特应用程序

from tkinter import *
import read_tag

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)                 
        self.master = master
        self.init_window()
    #Creation of init_window
    def init_window(self):
        # changing the title of our master widget      
        self.master.title("GUI")
        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)
        # creating a button instance
        readbutton = Button(self, text="Read Tag", command=read_tag.main)
        # placing the button on my window
        readbutton.place(x=0, y=0)
        outputlabel = Label(self, text="Tag Data :")
        outputlabel.place(x=60, y=0)
        output = Label(self, command=?)
        output.place(x=70, y=0)
root = Tk()
#size of the window
root.geometry("400x300")
app = Window(root)
root.mainloop()  

使用 output.config(text='UPDATED TEXT')

你可以尝试在tkinter中使用listbox很容易和清晰,看看这个例子:


from tkinter import *  
  
app = Tk()  
  
app.geometry("200x250")  
  
lbl = Label(app,text = "A list of favourite countries...")  
lbl.pack() 
listbox = Listbox(app)  
listbox.pack()  

listbox.insert(1,"text 1")  
  
listbox.insert(2, "text 2")  
  
listbox.insert(3, "text 3")  
  
listbox.insert(4, "text 4")  
  
 
  
app.mainloop()  

最新更新