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

0

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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)
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?
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 already
3

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

Comments

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.