Gabor
Gabor

Reputation: 25

Execute tkinter button without clicking on it

I have a Python GUI which makes a simple calculation. Running the main file called gui.py opens up a graphical interface. I would like to open the graphical interface and automatically click on the Kjør beregning button. (it means "Run calculation" in norwegian).

Button is defined the following way in gui.py:

beregn_btn = tk.Button(av_beregn, text="Kjør beregning", font=bold, command=self._beregn)

I'd like to add some code here to invoke calculation, if that is possible: So far without luck.

if __name__ == "__main__":
# Kjører program
root = KL_mast()
hovedvindu = Hovedvindu(root)
root.mainloop()

Upvotes: 1

Views: 302

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36732

You can do like this (small example that invokes the button without a click):

import tkinter as tk

def beregn():
    print('invoke_button called by button clicked or invoked')
    
def invoke_button():
    """ this does not call beregn, but instead invokes the beregn_btn"""
    beregn_btn.invoke()
    root.after(2000, invoke_button)
    
root = tk.Tk()
beregn_btn = tk.Button(root, text="Kjør beregning", command=beregn)
beregn_btn.pack()

root.after(2000, invoke_button)   # start the invocation demo

root.mainloop()

Upvotes: 1

Related Questions