在图形用户界面Tkinter Python中打印输出

2024-05-08 20:10:53 发布

您现在位置: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()

输出来自我运行代码的终端 我需要在图形用户界面的输出。


Tags: fromimporttkinterdefbuttonrootamsomething
2条回答

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

我想你没什么好放的了。在tkinter中,您需要一个标签来输出文本,或者您创建一个新的def并在那里定义按钮必须执行的操作(如果有人单击它)。

相关问题 更多 >