我想在弹出窗口 window 上打印我的输出?我应该如何处理?

I want to Print my output on pop up window? how should I approach?

import tkinter as tk
import datetime 

def write_slogan():
    dt_object = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print("OrderName: %s"%dt_object)


master = tk.Tk()
tk.Button(master, 
          text='ORDER Number', 
          command=write_slogan).grid(row=3, 
                                    column=1, 
                                    sticky=tk.W, 
                                    pady=4)

tk.mainloop()

我应该如何在弹出窗口 window 而不是终端上打印上述代码输出?

在 tkinter window 而不是终端中打印消息

如果您只想打印 root window 中的消息,您可以在以下代码中使用这样的 label 小部件:

import tkinter as tk

master = tk.Tk()

def write_slogan():
    info_message = "Your message"
    tk.Label(master, text=info_message).grid(row=2, column=1) 

btn = tk.Button(master, text='ORDER Number', command=write_slogan)
btn.grid(row=3, column=1, sticky=tk.W, pady=4)

master.mainloop()

在单独的弹出窗口中打印消息window

您可以使用 tkinter messagebox 模块中的消息框弹出一些信息,其中显示您的信息消息。

import tkinter as tk
import tkinter.messagebox as tkmb

def write_slogan():
    info_message = "Your message"
    # info message box
    tkmb.showinfo("Output", info_message)

master = tk.Tk()

btn = tk.Button(master, text='ORDER Number', command=write_slogan)
btn.grid(row=3, column=1, sticky=tk.W, pady=4)

master.mainloop()

创建顶层 window 作为弹出窗口 window

import tkinter as tk
import tkinter.messagebox as tkmb

def write_slogan():
    # toplevel window
    top_win = tk.Toplevel(master)
    top_win.title('Toplevel')
    info_message = "Your message"
    # info message box
    tk.Label(top_win, text=info_message).grid(row=0, column=0) 

master = tk.Tk()
master.title('Master')

btn = tk.Button(master, text='ORDER Number', command=write_slogan)
btn.grid(row=3, column=1, sticky=tk.W, pady=4)

master.mainloop()

因此,如果您有要打印的消息(水果)列表,您可以使用以下方法保存以前的结果并打印新结果

import tkinter as tk

window = tk.Tk()

# list of fruits
fruits = ['Mango', 'Apple', 'Orange', 'Banana', ]

#action to be performed when button clicked
def clicked():
    for ind, fruit in enumerate(fruits):
        # print names in the tkinter window
        # create a label widget
        names_label = tk.Label(window)
        # give it a position using grid
        names_label.grid(row=int(ind)+1, column=0)
        # print the fruit name in the label
        names_label.config(text=fruit)

btn = tk.Button(window, text="Print fruits", command=clicked)
btn.grid(column=0, row=0, padx=30, pady=2)

window.mainloop()

输出:

Click on Print fruits button to print all the fruit names

这里要理解的重要一点是 for 循环用于创建与列表中元素数量相等的 Label 小部件,列表项 index 是用于为这些标签小部件提供 rowcolumn 位置。

在顶层打印名称 window

import tkinter as tk

window = tk.Tk()

# list of fruits
fruits = ['Mango', 'Apple', 'Orange', 'Banana', ]

#action to be performed when button clicked
def clicked():
    # create a toplevel
    top_window = tk.Toplevel(window)
    for ind, fruit in enumerate(fruits):
        # print names in the tkinter window
        # create a label widget in top_window
        names_label = tk.Label(top_window)
        # give it a position using grid
        names_label.grid(row=int(ind)+1, column=0)
        # print the fruit name in the label
        names_label.config(text=fruit)

btn = tk.Button(window, text="Print fruits", command=clicked)
btn.grid(column=0, row=0, padx=30, pady=2)

window.mainloop()

输出: