Can I call a function by clicking 'OK' in messagebox.showinfo? - python

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

Related

The activebackground button function in tkinter, python is not working for macOS Big Sur Version: 11.6

This is my code:
import tkinter as tk
window=tk.Tk()
window.title("Click Me")
click=tk.Button(window, text="Click Me", highlightbackground="blue", activebackground="red")
click.place(x=200,y=300)
window.mainloop()
It doesn't return any error to me. But, when I click on the button, nothing happens.
Please reply to me as soon as you can.
I don't think the Mac allows you to change the background or activebackground of buttons. Apple wants to control the look of standard controls.
I have added two things to make this work:
a call back function helloCallback
pack the button properly so that it is visable.
here is the code:
import tkinter as tk
window=tk.Tk()
window.title("Click Me")
# a test function to execute when the button is clicked
def helloCallback():
print('hi there, it works')
return
click=tk.Button(window, text="Click Me", highlightbackground="blue", activebackground="red", command=helloCallback)
# pack the button
click.pack()
window.mainloop()
result: when the button is pressed, it prints hi there, it works to the console.

Combining Tkinter mainloop with another event listener

I am trying to build a program that listens for certain key combinations and then shows information to the user in a Tkinter window. To do this, I'm using a keylogger like so (simplified for this example):
from pyHook import HookManager
from pythoncom import PumpMessages
import Tkinter as tk
def on_keyboard_event(event):
label.config(text=event.Key)
root.update()
return True
hm = HookManager()
hm.KeyDown = on_keyboard_event
hm.HookKeyboard()
root = tk.Tk()
label = tk.Label(root, text='Hello world')
label.pack()
PumpMessages()
As expected, the window pops up and shows the user what key they pressed. However, I would like to integrate functionality to show other messages by interacting with the Tkinter window, such as by pressing a button. However, it seems I need Tkinter's mainloop to do this, which I can't figure out how to run alongside PumpMessages(), since it also halts the code similar to mainloop().
I tried running root.mainloop() in a root.after(), and I tried recreating root.mainloop like so:
def mainloop():
root.update()
root.after(50, mainloop)
and then running it right before PumpMessages, but neither of these solutions worked. It also doesn't seem like you can run PumpMessages or root.mainloop in a thread, though I could just not be doing it right. If this is not possible with Tkinter, is there an alternate Python GUI I could use that would make it possible?
You don't need to create a function to use mainloop() so just simply place the mainloop() at the bottom of your code. If you want a delay on it, use root.after(milliseconds, function)
Also, remember to put mainloop() before PumpMessages()
e.g.
def mainloopfunction():
mainloop()
root.after(5000, mainloopfunction)
Hope I could help!

string variable not working with

I have my main window, which i refer to as mainroot, and when i press a button the secondary window root should appear
in root there is an entry and i want it to be assigned to "hohoho" whenever i press anykey, so i tied it to KeyRelease event.
All i mentioned above is working when there is no mainroot, As in this code:
import tkinter as Tk
# mainroot=Tk.Tk()
root=Tk.Tk()
root.geometry("200x200")
myvar=Tk.StringVar()
entry=Tk.Entry(root,)
entry.pack()
entry.configure(textvariable=myvar)
entry.bind('<KeyRelease>',lambda event:printing())
def printing():
myvar.set("hohohohohohoho")
print('myvar is',myvar.get())
print('value is',entry.get())
root.mainloop()
# mainroot.mainloop()
but if you commented the root.mainloop() and uncommented root.mainloop() and # mainroot.mainloop() the text shown in entrystops following myvar.
what i found is that myvar is not working properly as it should.
anyone have any idea what am doing wrong or is this bug in tkinter or what?????
I know that entry.trace() is more elegant but i didn't use it because i am not very familiar with it.
thank you
any hint will be appreciated

Wait for button action on TkInter

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

Tkinter .pack() window not showing?

I've been working on a simple program that make a button output something. But when i run it,
this
(I got this from the internet btw) does not show up. Is somethoing wrong with the code or something?
Please help me so the window above can appear :)
Code:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
You forgot to call the Tk.mainloop method at the end of your program:
from Tkinter import *
def asdf():
print('test')
tk = Tk()
b = Button(tk, text="test", command=asdf)
b.pack()
##############
tk.mainloop()
##############
Doing so starts Tkinter's main event loop and creates the window.
It seems you are using Python3, as there are parentheses after print, so from Tkinter import * should be from tkinter import *. Python is case-sensitive. You also forgot to call root.mainloop() at the end of your code as #user2555451 mentioned, although a window should appear all the same, but stop responding when any event occurs (e.g., clicks, key presses, focus changes).

Categories