Print output in GUI interface Tkinter Python
Asked Answered
A

3

6
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.

Avram answered 16/3, 2017 at 8:6 Comment(0)
K
8

Using print only prints to the terminal or a fp. You can create a new Label to "print" to the 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()
Kirkendall answered 16/3, 2017 at 8:13 Comment(4)
hello @abccd bro, for just an example what if i do: for x in range(0,9): print x and i want the value of x to print in the GUI? how will i do it?Avram
Well.. you can check out my new edit @AnandVyas. If I'm not mistaken, this creates a row of labels from 0 to 8. (I can't try it out right now)Kirkendall
Thanks bro it worked! But the print flow is bad if i print 200 numbers in a line the whole desktop will fill up what should i do?Avram
A easy way is though create the labels to a frame and set the size of window and create a scroll bar. But that's a different question that you can ask or find a question that has been asked alreadyKirkendall
S
2

Use the following code to get output in GUI interface.

from tkinter import *

def printSomething():
    content = "Hey whatsup bro, i am doing something very interresting."
    lb.config(text=content) 

root = Tk()
root.geometry('600x600')
lb = Label(root,text="")
lb.pack()
button = Button(root, text="Print Me", command=printSomething)
button.pack()

root.mainloop()
Semblance answered 29/11, 2023 at 10:25 Comment(0)
I
-1

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.

Ingeminate answered 16/3, 2017 at 8:10 Comment(1)
for just an example what if i do: for x in range(0,9): print x and i want the value of x to print in the GUI? how will i do it? any examples with codes?Avram

© 2022 - 2024 — McMap. All rights reserved.