问题
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()
回答1:
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()
来源:https://stackoverflow.com/questions/64892616/execute-tkinter-button-without-clicking-on-it