Execute tkinter button without clicking on it

人盡茶涼 提交于 2021-01-28 18:10:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!