执行tkinter按钮而不点击它 [英] Execute tkinter button without clicking on it
本文介绍了执行tkinter按钮而不点击它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Python GUI,可以进行简单的计算.运行名为 gui.py
的主文件将打开一个图形界面.我想打开图形界面并自动单击Kjørberegning按钮
.(在挪威语中表示运行计算").
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).
按钮
在 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()
推荐答案
您可以这样做(一个小的示例,无需单击即可调用该按钮):
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()
这篇关于执行tkinter按钮而不点击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文