我有一个函数fonc(a,b,c,bool1,bool2),它接受float(a,b,c)和布尔参数。 此函数可以根据布尔值和浮点值绘制不同类型的图形(使用matplotlib)。在
我试图在tkinter中为这个函数制作一个“图形界面”。在这个界面中,我可以用一个复选框来改变布尔值,用一个旋转框和一个按钮“启动”来改变布尔值,这个按钮用当前参数和界面中的绘图来启动函数。在
我试着先用一个复选框来做,问题是,函数put the part命令的按钮启动是静态的。我的意思是它接受我放置的默认参数,即使使用我的复选框来更改参数的值,它也不会更改。在
这是我的代码,函数是testpanneau.testpanneau1
,布尔值是toutedir2
:
import tkinter as tk
import testpanneau
from functools import partial
"""constantes"""
latitude = 37.77
longitude = -122.42
j = 21
m = 12
a = 2016
timezone = -8
hauteurlieu = 0.282
costheta = False
toutedir1 = False
toutedir2 = True
infonord = False
cst = 32
"""definition de la fonction de changement de booleen"""
def chgtbool1():
print("Button clicked")
toutedir2 = True if is_checked.get() else False
print(toutedir2)
"""creation de ma fenetre"""
mafenetre = tk.Tk()
mafenetre.title('Test irradiance')
mafenetre.geometry('600x200')
"""definition des boolen des checkbox"""
#toutedir2
is_checked = tk.BooleanVar()
#toutedir1
#costheta
#infonord
"""creation des checkbox"""
checkbox = tk.Checkbutton(mafenetre, variable=is_checked, command=chgtbool1)
"""Création d'un widget Button (bouton Quitter)"""
Bouton1 = tk.Button(mafenetre, text = 'Quitter', command = mafenetre.destroy)
""" Création d'un widget Button (bouton Lancer)"""
BoutonLancer = tk.Button(mafenetre, text ='generer', command = partial(testpanneau.testpanneau1,latitude,longitude,j,m,a,timezone,hauteurlieu,cst,costheta,toutedir1,toutedir2,infonord))
"""Positionnement du widget avec la méthode pack()"""
checkbox.pack(side = tk.LEFT, padx = 5, pady = 7)
Bouton1.pack()
BoutonLancer.pack(side = tk.RIGHT, padx = 5, pady = 0)
""" Lancement du gestionnaire d'événements"""
mafenetre.mainloop()
仍然不确定目标,但是这个代码片段演示了如何更新按钮的命令(动态),这是本问题的标题。在
当您创建
partial()
时,它是用变量at the time it is created的值创建的:如这个简单的例子所示:
单击文本框的次数不会改变运行
command
时看到的值。在当您创建分部时,您需要做的是传入
is_checked
,然后在函数testpanneau.testpanneau1
中,执行is_checked.get()
以获得布尔值。您也可以取消chgtbool1()
函数。在这段代码正常工作,我用
^{pr2}$testpanneau.testpanneau1
替换了myfun()
,当按下generer
按钮时,复选框的值就正确地打印出来了。在抱歉,我不得不把你的评论翻译成英语,因为我的python2.7解释器在抱怨重音字符
我通过使用一个函数upade来解决我的问题,该函数用一个新的分部函数更新了我的命令:
感谢大家的帮助:)
相关问题 更多 >
编程相关推荐