如何更新tkinter按钮命令?

2024-09-24 12:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数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()

Tags: 函数importfalse参数debutton按钮pack
3条回答

仍然不确定目标,但是这个代码片段演示了如何更新按钮的命令(动态),这是本问题的标题。在

def generer() :
    Boutonint = tk.Button(mafenetre, text = 'lancer', command = someAction)
    Boutonint.pack(side = tk.RIGHT, padx = 3, pady = 0)

def noPlot():
    BoutonLancer['command'] = noPlot
    print("Making plot...")

def someAction():
    print('lancer button caused this')

"""definition de la fonction de changement de booleen"""
def chgtbool1():
    print("Button clicked")
    toutedir2 = is_checked.get()
    if toutedir2:
        BoutonLancer['command'] = generer
    else:
        BoutonLancer['command'] = noPlot
    print(toutedir2)

当您创建partial()时,它是用变量at the time it is created的值创建的:

The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature....

如这个简单的例子所示:

>>> from functools import partial
>>> i = 3
>>> def prt(a):
...     print a
...
>>> x = partial(prt, i)
>>> x()
3
>>> i = 5
>>> x()
3

单击文本框的次数不会改变运行command时看到的值。在

当您创建分部时,您需要做的是传入is_checked,然后在函数testpanneau.testpanneau1中,执行is_checked.get()以获得布尔值。您也可以取消chgtbool1()函数。在

这段代码正常工作,我用testpanneau.testpanneau1替换了myfun(),当按下generer按钮时,复选框的值就正确地打印出来了。在

^{pr2}$

抱歉,我不得不把你的评论翻译成英语,因为我的python2.7解释器在抱怨重音字符

我通过使用一个函数upade来解决我的问题,该函数用一个新的分部函数更新了我的命令:

def maj():
    global BoutonLancer
    BoutonLancer['command'] = partial(testpanneau.testpanneau1,latitude,longitude,j,m,a,timezone,hauteurlieu,cst,costheta,toutedir1,toutedir2,infotheta,beta,gamma)

感谢大家的帮助:)

相关问题 更多 >