Hi guys im trying to make a subprogram that waits for a button to be pressed in order to continue, in othe words: a loop that has a condidtinal, beeing the conditianal having a button pressed, and I have no idea of what can i use to do that.
Here is an easy example of what others have said, this function waits until you hit the button, when it will run the function written:
import tkinter as tk
root = tk.Tk()
root.title('waiting for button click...')
def carry_on:
# do what ever
button = tk.Button(text = 'click to continue', command = carry_on)
button.pack()
root.mainloop()
if you have a more specific question please add more information :)
Related
import tkinter
win=tkinter.Tk()
win.configure(background='grey')
k=False
def g():
k=True
v=tkinter.Button(win, text='click', command=g)
v.pack()
while k==True:
win.configure(background='black')
win.mainloop()
There's no reason why that while loop would run after the button is clicked, since (as you know) your program is run "from top to bottom", and control remains in win.mainloop() until the window is closed. (You can find that out by adding print("bye!") after that call.)
You might want to just directly call .configure(). (I gave the button some padding here so you can see the background change; otherwise the button may take up the entirety of the window and you won't see a change.)
import tkinter
win = tkinter.Tk()
def change_color():
win.configure(background='black')
button = tkinter.Button(win, text='click', command=change_color)
button.pack(padx=10, pady=10)
win.mainloop()
Now I am going to design a little GUI game like find a pair. And I want to add the sound effect when I click every buttons on it. But I don't know how to add these sound. As the previous answer
How can I play a sound when a tkinter button is pushed?
said, I need to defined the button as this way:
Button(root, text="Play music", command=play_music).pack()
The button has another feature.
Button(game_window,image=blank_image,command=cell_0).grid(row=1,column=1)
So how 'command=play_music' should be placed?
I think you want to alter your functions with a common functionality. For this decorators are really good. A decorator allows you to add functionality to existing functions. An exampel can be found below:
import tkinter as tk
import winsound as snd
def sound_decorator(function):
def wrapped_function(*args,**kwargs):
print('I am here')
snd.PlaySound('Sound.wav', snd.SND_FILENAME)
return function(*args,**kwargs)
return wrapped_function
#sound_decorator
def cmd():
print('command')
root = tk.Tk()
button = tk.Button(root, text = 'Play', command = cmd)
button.pack()
root.mainloop()
make a change like this:
Button(game_window,image=blank_image,command=lambda:[cell_0(),play()]).grid(row=1,column=1)
Then it works.
I know there are better ways about this, but I cant figure out what's
wrong about this code, or at least, why it wont function the way I want it. Currently I made a simple test program to try my concept away from my
main code.
from tkinter import *
root = Tk()
test = True
def click():
global test
print("working")
test = False
button = Button(root, text="Hi", command=click)
if test:
button.pack()
root.mainloop()
Everything runs fine but when I press the button all I get is the message "working" without the button going away.
In your code python checks if test is True and as it is, it packs the button and moves on. What you need to use is <tkinter widget>.pack_forget(). It removes the widget from the screen without destroying it. If you later call pack it should put it back in its original place. This is your code with the pack_forget:
from tkinter import *
root = Tk()
def click():
print("working")
button.pack_forget()
button = Button(root, text="Hi", command=click)
button.pack()
root.mainloop()
I have added a message box in my code and I want to call a function defined earlier when clicking the 'OK' button in the message box. Is there any way to achieve this? Thanks in advance.
messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')
When you make a showinfo box, it actually halts your script until you press OK. All you will need to do is put the function you want right after it.
See this example:
import tkinter as tk
from tkinter import messagebox
def go():
messagebox.showinfo('Correct', 'Correct!\nClick OK to continue')
print('yeah')
root = tk.Tk()
btn = tk.Button(root, text='click', command=go)
btn.pack()
root.mainloop()
If you run that, you will see the print('yeah') only happens when the user clicks OK. So, you can do something similar and call the function where I have put the print.
I had a similar problem and looked for a simple code. You can use:
from tkinter import Tk,messagebox
root=Tk()
result=messagebox.showinfo('Correct','Correct!')
root.destroy()
go()
I used root.destroy() because sometimes I had problems closing the popup window.
In tkinter.messagebox.showinfo your only option is to press OK or close the tkinter popup window, both has the same effect and returns a string "ok".
If you instead want to call a function only when you press yes use tkinter.messagebox.askyesno:
from tkinter import Tk,messagebox
root=Tk()
result=messagebox.askyesno('Correct','Correct!')
root.destroy()
if result==True:
go()
I would like to press a button and have it print the location of the cursor X/Y coordinates as well as the content of the button in tkinter.
I'm new to python and I haven't found any way to do this. Can anyone help me finding the right start on this issue?
You can use the bind the button click event to print the location clicked, here's an example of how you can do that.
import tkinter as tk
def print_location(event):
print(event.x, event.y)
root = tk.Tk()
button = tk.Button(root, text='click me')
button.bind("<Button-1>", print_location)
button.pack()
root.mainloop()
Note: using external modules such as pyautogui is not recommended and unnecessary.