在 GUI 界面 Tkinter Python 中打印输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42828416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:11:14  来源:igfitidea点击:

Print output in GUI interface Tkinter Python

pythonuser-interfacetkinter

提问by Anand Vyas

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()

The output is coming in the terminal from where i am running the code I need the output in GUI interface.

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

回答by abccd

Using print only prints to the terminal or a fp. You can create a new Label to "print" to the GUI.

使用 print 仅打印到终端或 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()

AN EXAMPLE FOR YOUR COMMENT

您的评论示例

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()

回答by xFuture

I think you have nothing to put your "Hey whatsup bro, i am doing something very interresting."in. In tkinter you need something like a Label to put text out, or you create a new def and define there what the button have to do, if someone click on it.

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