在GUI界面Tkinter Python中打印输出


from Tkinter import *
def printSomething():
    print "Hey whatsup bro, i am doing something very interresting."
root = Tk()
button = Button(root, text="Print Me", command=printSomething)
button.pack()
root.mainloop()

输出来自我运行代码的终端我需要 GUI 界面中的输出。

使用打印仅打印到终端或 fp。您可以创建一个新标签以"打印"到 GUI。

from Tkinter import *
def printSomething():
    # if you want the button to disappear:
    # button.destroy() or button.pack_forget()
    label = Label(root, text= "Hey whatsup bro, i am doing something very interresting.")
    #this creates a new label to the GUI
    label.pack() 
root = Tk()
button = Button(root, text="Print Me", command=printSomething) 
button.pack()
root.mainloop()

您的评论示例

from Tkinter import *
def printSomething():
    # if you want the button to disappear:
    # button.destroy() or button.pack_forget()
    for x in range(9): # 0 is unnecessary
        label = Label(root, text= str(x))
    # this creates x as a new label to the GUI
        label.pack() 
root = Tk()
button = Button(root, text="Print Me", command=printSomething) 
button.pack()
root.mainloop()

我认为你没有什么可以放"Hey whatsup bro, i am doing something very interresting."的。在 tkinter 中,您需要类似标签的东西来放置文本,或者您创建一个新的 def 并在那里定义按钮必须做什么,如果有人点击它。

最新更新