When I click a button, it should write text under it and the text should disappear after few seconds.
I don't know how to code that. What I have tried so far:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
time.sleep(5)
tex.destroy()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
You can use .after() method to destroy the label after fixed period of time.
The following example will delete the label after 3 seconds:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
tex.after(3000, tex.destroy)
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
Output:
Your code looks correct for the most part. The reason why it doesn't appear to be working is that there is nothing telling the window to update after adding the text. A simple fix would be to add window.update() when you create the label.
The Code should look like:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
window.update()
time.sleep(5)
tex.destroy()
window.update()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
Related
What my code does currently is create a label with the text "Hello" every time I press the button that says "Say hello"
What I'm trying to figure out is how to create a button that clears off all of the labels off the screen, however I'm completely clueless.
How do I create a button that clears all of the labels off of the screen?
My code is down below.
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
root.mainloop()
I'm beginner but try this:
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
liste = []
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
liste.append(helloLabel)
def DelLabel():
for i in range(len(liste)):
liste[i].destroy()
liste.clear()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
labelButton = tk.Button(root, text="del hello", command=DelLabel)
labelButton.pack()
root.mainloop()
I just new for the GUI and need little help.
a=int(input())
if a==0:
print("hi")
else:
print("hello")
I want to change input process to click button, like a switch.
left button -> a=0
right button -> a=1
window=tkinter.Tk()
window.title("")
window.geometry("640x640+100+100")
window.resizable(True, True)
a=tkinter.Button(window, text="left")
a.pack()
b=tkinter.Button(window, text="right")
b.pack()
window.mainloop()
I can see left, right button but I don't know how to put values.
Is there any example I can use?
Thanks
Does this example help You:
from tkinter import Tk, Button
def switch(btn1, btn2):
btn1.config(state='disabled')
btn2.config(state='normal')
print(btn1['text'])
window = Tk()
window.title("")
on = Button(window, text="On", command=lambda: switch(on, off))
on.pack(side='left', expand=True, fill='x')
off = Button(window, text="Off", command=lambda: switch(off, on))
off.pack(side='left', expand=True, fill='x')
off.config(state='disabled')
window.mainloop()
If You have questions ask but here is pretty good site to look up tkinter widgets and what they do, their attributes.
Also I suggest You follow PEP 8
You need to add a function to each one that will be executed when the buttons are clicked like this:
import tkinter as tk
def left_clicked():
print("Left button clicked")
right_button.config(state="normal") # Reset the button as normal
left_button.config(state="disabled") # Disable the button
def right_clicked():
print("Right button clicked")
right_button.config(state="disabled")
left_button.config(state="normal")
window = tk.Tk()
window.title("")
window.geometry("640x640+100+100")
# window.resizable(True, True) # Unneeded as it is already the default
left_button = tk.Button(window, text="left", command=left_clicked)
left_button.pack(side="left")
right_button = tk.Button(window, text="right", command=right_clicked,
state="disabled")
right_button.pack(side="right")
window.mainloop()
I've started learning Tkinter on Python few weeks ago and wanted to create a Guess Game. But unfortunately I ran into a problem, with this code the text for the rules ( in the function Rules; text='Here are the rules... ) doesn't appear on the window ( rule_window).
window = Tk()
window.title("Guessing Game")
welcome = Label(window,text="Welcome To The Guessing Game!",background="black",foreground="white")
welcome.grid(row=0,column=0,columnspan=3)
def Rules():
rule_window = Tk()
rule_window = rule_window.title("The Rules")
the_rules = Label(rule_window, text='Here are the rules...', foreground="black")
the_rules.grid(row=0,column=0,columnspan=3)
rule_window.mainloop()
rules = Button(window,text="Rules",command=Rules)
rules.grid(row=1,column=0,columnspan=1)
window.mainloop()
Does anyone know how to solve this problem?
In your code you reset whatever rule_window is to a string (in this line: rule_window = rule_window.title(...))
Try this:
from import tkinter *
window = Tk()
window.title("Guessing Game")
welcome = Label(window, text="Welcome To The Guessing Game!", background="black", foreground="white")
welcome.grid(row=0, column=0, columnspan=3)
def Rules():
rule_window = Toplevel(window)
rule_window.title("The Rules")
the_rules = Label(rule_window, text="Here are the rules...", foreground="black")
the_rules.grid(row=0, column=0, columnspan=3)
rules = Button(window, text="Rules", command=Rules)
rules.grid(row=1, column=0, columnspan=1)
window.mainloop()
When you want to have 2 windows that are responsive at the same time you can use tkinter.Toplevel().
In your code, you have initialized the new instances of the Tkinter frame so, instead of you can create a top-level Window. What TopLevel Window does, generally creates a popup window kind of thing in the application. You can also trigger the Tkinter button to open the new window.
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")
#Define a function
def open_new():
#Create a TopLevel window
new_win= Toplevel(root)
new_win.title("My New Window")
#Set the geometry
new_win.geometry("600x250")
Label(new_win, text="Hello There!",font=('Georgia 15 bold')).pack(pady=30)
#Create a Button in main Window
btn= ttk.Button(root, text="New Window",command=open_new)
btn.pack()
root.mainloop()
I'm trying to make a little pop-up interface before my game to choose difficulties. The problem I'm having is that after I click yes on the first messagebox.askyesno() the whole pop up disappears and I have no idea on how to know which button was pressed to return an output. This is my first time using tkinter and messagebox so any help is greatly appreciated.
This is my code:
import tkinter as tk
from tkinter import messagebox
def start_game():
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
if messagebox.askyesno("Welcome to Snake!", "Would you like to play?") == True:
mainLabel = tk.Label(root, text='Choose a dificulty:')
easy_ask = tk.Button(root, text='Easy')
medium_ask = tk.Button(root, text='Medium')
hard_ask = tk.Button(root, text='Hard')
mainLabel.pack(side=tk.LEFT)
easy_ask.pack(side=tk.LEFT)
medium_ask.pack(side=tk.LEFT)
hard_ask.pack(side=tk.LEFT)
root.deiconify()
root.destroy()
root.quit()
root.mainloop()
start_game()
What about an else statement ;-)? Consider what your code is doing: if you click 'Yes', it creates some Button on a main window that you have withdrawn. Then, immediately after you deiconify it, you destroy it. Reshuffle your code like follows:
import tkinter as tk
from tkinter import messagebox
def start_game():
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
if messagebox.askyesno("Welcome to Snake!", "Would you like to play?") == True:
root.deiconify() # <--- deiconify under the True condition
mainLabel = tk.Label(root, text='Choose a dificulty:')
easy_ask = tk.Button(root, text='Easy')
medium_ask = tk.Button(root, text='Medium')
hard_ask = tk.Button(root, text='Hard')
mainLabel.pack(side=tk.LEFT)
easy_ask.pack(side=tk.LEFT)
medium_ask.pack(side=tk.LEFT)
hard_ask.pack(side=tk.LEFT)
else:
root.destroy() # <--- destroy and quit under the False condition
root.quit()
root.mainloop()
start_game()
Note that you can also assign the outcome of messagebox.askyesno to a variable:
answer = messagebox.askyesno("Welcome to Snake!", "Would you like to play?")
Noob Alert!!!
Hello, I just started my journey with through python a couple of days ago, so my question will most likely be extremely simple to answer. Basically I have a random number gen. from 1 to 10, this is activated when the "test button is pressed on the windows that pops up. As you can see from the image below the random number appears in the output console on the bottom of the screen, in this case it was a 9. So here's the question, How can I make the random number on the GUI? so when the button is pressed a random number appears on the same window as the button.
https://i.stack.imgur.com/hWd3i.png
Any help is appreciated!
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
root.mainloop()
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
updateDisplay(value)
def updateDisplay(myString):
displayVariable.set(myString)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
displayVariable = StringVar()
displayLabel = Label(root, textvariable=displayVariable)
displayLabel.pack()
root.mainloop()
Here is what it looks like.You have to create a Label with a Button, whose value will get updated when you click on button.
import tkinter as tk
from random import randint
win = tk.Tk()
def test_button_click():
label_val.set(randint(1, 10))
my_button = tk.Button(win, text='Test Button',
command=test_button_click)
my_button.grid(column=0, row=0)
label_val = tk.IntVar()
my_Label = tk.Label(win, textvariable=label_val)
my_Label.grid(column=1, row=0)
win.mainloop()
This will achieve what you are requesting -- create a tk window, add a button and label, use the callback test_button_click to set the labels int var when the button is clicked.