Print output in GUI interface Tkinter Python - 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()
The output is coming in the terminal from where i am running the code
I need the output in GUI interface.

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

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.

Related

how to solve this: how to clear the tkinter label with button

I'm using tkinter.
I have a button to create a label with text in it. I want to clear that label with another button.
How can i do that?
I'm pretty new on tkinter. ıs there somebody to lead me
Here are a few options
Option 1
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text='Clear Me!')
label.pack()
# define a function to set the label text to an empty string
def clear_label_text():
label.config(text='')
# set up a button to call the above function
btn_clear = tk.Button(
root,
text='Click to Clear Label',
command=clear_label_text # note the lack of '()' here!
)
btn_clear.pack()
if __name__ == '__main__':
root.mainloop() # start the app
Option 2
This is a similar approach, but instead of defining a function to clear the label, we'll use a lambda (an anonymous function)
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text='Clear Me!')
label.pack()
# set up a button to clear the label
btn_clear = tk.Button(
root,
text='Click to Clear Label',
# lambdas make for clean code, but don't go crazy!
command=lambda: label.config(text='')
)
btn_clear.pack()
if __name__ == '__main__':
root.mainloop() # start the app
Neither of these methods will outright destroy the label, so you can set it's text again at any time a la label.config(text='New and improved text!')
from tkinter import *
root = Tk()
lab1 = Label(root, text = "Hello World")
lab1.pack()
but1 = Button(root, text = "clear", command=lab1.destroy)
but1.pack()
root.mainloop()

How can I make it so the text boxes in tkinter are created on a canvas and not on the window itself?

I am trying to make it so whenever you press the "+" button, it creates a text box on the canvas that is inside of the window. But, whenever I try to do that, it just puts it on the window itself and not on the canvas. Here is the code. Any help would be greatly appreciated!
from tkinter import *
from random import choice
root = Tk()
root.title("TBCITW")
root.geometry('340x800')
root.resizable (1,1)
canvasmain=Canvas(
root,
bg="#1c1c1c",
width=340,
height=10000,)
canvasmain.place(x=-2, y=41,)
def plusentrymain():
global textplus
textplus=Entry(canvasmain).grid(padx=5, pady=5)
#Buttons
button1=Button(root, text=" + ", padx=2,command=plusentrymain)
button1.place(x=10, y=10)
root.mainloop()
I changed the widget canvasmain to root in line 21. Outside of the function, I added an Entry. 
Python is the rule. I altered the code to make it readable.
from tkinter import *
from random import choice
root = Tk()
root.title("TBCITW")
root.geometry('340x800')
root.resizable (1,1)
canvasmain=Canvas(
root,
bg="#1c1c1c",
width=340,
height=10000,
)
canvasmain.place(x=2, y=41,)
def plusentrymain():
global textplus
textplus=Entry(root)
textplus.grid(padx=5, pady=5)
#Buttons
button1=Button(root, text=" + ", padx=2, command=plusentrymain)
button1.place(x=10, y=10)
#Entry
textplus=Entry(root)
textplus.grid(padx=5, pady=45)
root.mainloop()
Output:
Before click..
After click.

How to change the visibility of a button in tkinter?

I'm new to python and I'm trying to create an application in which I want a button to be visible only after I click the "show" button. The button should not be visible from the start of application it should only be visible after clicking on "show" button.
I have this code which hides the button after clicking on another button. It changes its text to "show" after hiding the button.
from tkinter import *
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()
I want the button to show only after I click on the "show" button, not from the start.
At the risk of stating the obvious, if you don't want the button to be visible at startup, don't show the button at startup.
You're explicitly making the button visible with this line of code near the start of the program:
btn1.place(x=20, y=50)
You're then setting btn1.pi by calling place_info, but you don't need to do that. You can directly set btn1.p without first calling .place followed by place_info.
btn1 = Button(root,text="Example")
btn1.visible = False
btn1.pi = {"x": 20, "y": 50}
Notice that I also changed btn1.visible to False. You don't actually need a separate attribute to track if it's visible, tkinter can answer that question with the method winfo_viewable().
Or, you can simply remove the button after calculating btn1.pi:
btn1.place(x=20, y=50)
btn1.visible = False
btn1.pi = btn1.place_info()
btn1.place_forget()
I know this is not for the question I had a hard time finding this. If you are using the GTK library then
ButtonName.set_visible(False) sets it invisible
the below is for tkinter I just tested the below for tkinter:
from tkinter import *
def hide_me(event):
event.widget.pack_forget()
def make_invisible(widget):
widget.pack_forget()
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
make_invisible(btn2)
root.mainloop()
the make_invisible(btn2) method sets it invisible however make sure to call it after you pack the button otherwise it will still show up.
here is my resources I used to get to this code
https://www.tutorialspoint.com/how-to-make-a-tkinter-widget-invisible
and https://newbedev.com/in-tkinter-is-there-any-way-to-make-a-widget-not-visible
i hope that this is what you mean:
from tkinter import *
import tkinter
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
btnToggle1["state"] = DISABLED
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["state"] = NORMAL
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()

How to make a Label appear then disappear after a certain amount of time in python tkinter

I would like for a cant afford label to appear then after a second disappear when i push a button to buy something. It seems like the time.sleep(1) is making it not work properly. This is done on python tkinter.
def buttonpressed():
Label.place(x = 500, y = 500 #where i want the label to appear
time.sleep(1)
Label.place(x = 10000, y = 10000) #moving it away where i wont be able to see it
You can't use sleep because it stops mainloop
You can use root.after(time_in_milliseconds, function_name) to run function
Example
import tkinter as tk
def button_pressed():
# put text
label['text'] = "Hello World!"
# run clear_label after 2000ms (2s)
root.after(2000, clear_label)
def clear_label():
# remove text
label['text'] = ""
root = tk.Tk()
label = tk.Label(root) # empty label for text
label.pack()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
If you have to create and remove label then use label.destroy()
import tkinter as tk
def button_pressed():
label = tk.Label(root, text="Hello World!")
label.pack()
root.after(2000, destroy_widget, label) # label as argument for destroy_widget
def destroy_widget(widget):
widget.destroy()
root = tk.Tk()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
And shorter version without destroy_widget
import tkinter as tk
def button_pressed():
label = tk.Label(root, text="Hello World!")
label.pack()
root.after(2000, label.destroy)
root = tk.Tk()
button = tk.Button(root, text="Press Button", command=button_pressed)
button.pack()
root.mainloop()
Press button many times to see many labels which disappear after 2s.
You can use after() to set up a callback after a specified interval. In the callback function clear the label with pack_forget() (or grid_forget() if you're using grid). This is better than setting the label's text attribute to an empty string because that causes widgets to be resized, which might not be what you want. Here's an example:
import Tkinter as tk
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text='I am a label')
self.label.place(x=0, y=0)
self.label.after(1000, self.clear_label) # 1000ms
self.root.mainloop()
def clear_label(self):
print "clear_label"
self.label.place_forget()
app=App()
Another option is to use self.label.destroy() to destroy the widget, however, pack_forget() allows you to display the label again by calling pack() on the widget again.
# I made it through calling 2 functions:
from tkinter import *
root = Tk()
root.geometry('400x200') # width by height
def one_text():
label['text'] = "Look around"
root.after(1000, another_text)
def another_text():
label['text'] = "and smile"
root.after(1000, one_text)
label= Label(root ,font=('Helvetica', 14), fg='black', bg='white')
label.pack()
one_text()
root.mainloop()

New windows in tkinter

I have a bit of difficulty with the code below. Basically, I want the code to, when I press the Enter button, to open the window2 but also close window1 simultaneously so that there is only one window and not two of them.
The code is...
from tkinter import *
def window1():
window = Tk()
window.title("Welcome")
f = Frame()
f.pack()
label1 = Label(window, text = "Welcome to the random window")
label1.pack()
button1 = Button(window, text = "Enter...", command = window2)
button1.pack()
def window2():
screen = Tk()
screen.title("Pop-Up!")
fr = Frame()
fr.pack()
label2 = Label(screen, text = "This is a pop-up screen!")
label2.pack()
button2 = Button(screen, text = "Return", command = window1)
button2.pack()
window1()
This is "Bad" because you're using two instances of Tk. Try instead using TopLevels.
import tkinter as tk
def window1():
window = tk.Toplevel(root)
window.title("Welcome")
# etc etc ...
tk.Button(window,text="Enter...",command=lambda: window2(window)).pack()
def window2(old_window):
old_window.destroy()
# window2 stuff
root = tk.Tk()
root.iconify() # to minimize it, since we're just using Toplevels on top of it
window1()
root.mainloop()
When you are using the Tk() function, you are creating a new instance of the Tcl/tkinter interpreter. Instead use Toplevel() which will make a new window in the current interpreter.

Categories

Resources