第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么在聲明時(shí)執(zhí)行Button參數(shù)“command”?

為什么在聲明時(shí)執(zhí)行Button參數(shù)“command”?

BIG陽(yáng) 2019-05-22 14:01:41
為什么在聲明時(shí)執(zhí)行Button參數(shù)“command”?我的代碼是:from Tkinter import *admin = Tk()def button(an):    print an    print 'het'b = Button(admin, text='as', command=button('hey'))b.pack()mainloop()按鈕不起作用,它在沒(méi)有我的命令的情況下打印'hey'和'het',然后,當(dāng)我按下按鈕時(shí)沒(méi)有任何反應(yīng)。
查看完整描述

4 回答

?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

您需要?jiǎng)?chuàng)建一個(gè)沒(méi)有參數(shù)的函數(shù),您可以將其用作命令:

b = Button(admin, text='as', command=lambda: button('hey'))

請(qǐng)參閱本文檔的“將參數(shù)傳遞給回調(diào)”部分。


查看完整回答
反對(duì) 回復(fù) 2019-05-22
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

當(dāng)引擎在“... command = ...”行分配值時(shí),引擎會(huì)評(píng)估函數(shù)的結(jié)果。


“命令”期望返回一個(gè)函數(shù),這就是為什么使用lambda可以完成這項(xiàng)工作,因?yàn)樗鼊?chuàng)建了一個(gè)在評(píng)估期間返回“命令”的異常函數(shù)。您也可以編寫自己的功能,它也可以完成這項(xiàng)工作。


這是一個(gè)lambda和沒(méi)有l(wèi)ambda的例子:


#!/usr/bin/python

# coding=utf-8


from Tkinter import *

# Creation de la fenêtre principale (main window)

Mafenetre = Tk()

res1 = StringVar()

res2 = StringVar()


def isValidInput(obj):

    if hasattr(obj, 'get') and callable(getattr(obj, 'get')):

        return TRUE

    return FALSE



# stupid action 2 (return 12 on purpose to show potential mistake)

def action1(*arguments):

    print "action1 running"

    for arg in arguments:

        if isValidInput(arg):

            print "input value: ", arg.get()

            res1.set(arg.get())

        else:

            print "other value:", arg

    print "\n"

    return 12



# stupid action 2

def action2(*arguments):

    print "action2 running"

    a = arguments[0]

    b = arguments[1]

    if isValidInput(a) and isValidInput(b):

        c = a.get() + b.get()

        res2.set(c)

        print c

    print "\n"



# a stupid workflow manager ordered by name

def start_tasks(*arguments, **keywords):

    keys = sorted(keywords.keys())

    for kw in keys:

        print kw, "plugged "

        keywords[kw](*arguments)



# valid callback wrapper with lambda

def action1_callback(my_input):

    return lambda args=[my_input]: action1(*args)



# valid callback wrapper without lambda

def action1_callback_nolambda(*args, **kw):

    def anon():

        action1(*args)

    return anon



# first input string

input1 = StringVar()

input1.set("delete me...")

f1 = Entry(Mafenetre, textvariable=input1, bg='bisque', fg='maroon')

f1.focus_set()

f1.pack(fill="both", expand="yes", padx="5", pady=5)


# failed callback because the action1 function is evaluated, it will return 12. 

# in this case the button won't work at all, because the assignement expect a function 

# in order to have the button command to execute something

ba1 = Button(Mafenetre)

ba1['text'] = "show input 1 (ko)"

ba1['command'] = action1(input1)

ba1.pack(fill="both", expand="yes", padx="5", pady=5)


# working button using a wrapper

ba3 = Button(Mafenetre)

ba3['text'] = "show input 1 (ok)"

# without a lambda it is also working if the assignment is a function

#ba1['command'] = action1_callback_nolambda(input1)

ba3['command'] = action1_callback(input1)

ba3.pack(fill="both", expand="yes", padx="5", pady=5)


# display result label

Label1 = Label(Mafenetre, text="Action 1 result:")

Label1.pack(fill="both", expand="yes", padx="5", pady=5)

# display result value

resl1 = Label(Mafenetre, textvariable=res1)

resl1.pack(fill="both", expand="yes", padx="5", pady=5)



# second input string

input2 = StringVar()

f2 = Entry(Mafenetre, textvariable=input2, bg='bisque', fg='maroon')

f2.focus_set()

f2.pack(fill="both", expand="yes", padx="5", pady=5)


# third test without wrapper, but making sure that several arguments are well handled by a lambda function

ba2 = Button(Mafenetre)

ba2['text'] = "execute action 2"

ba2['command'] = lambda args=[input1, input2], action=action2: start_tasks(*args, do=action)

ba2.pack(fill="both", expand="yes", padx="5", pady=5)


# display result label

Label2 = Label(Mafenetre, text="Action 2 result:")

Label2.pack(fill="both", expand="yes", padx="5", pady=5)

# display result value

resl2 = Label(Mafenetre, textvariable=res2)

resl2.pack(fill="both", expand="yes", padx="5", pady=5)


Mafenetre.mainloop()


查看完整回答
反對(duì) 回復(fù) 2019-05-22
  • 4 回答
  • 0 關(guān)注
  • 1403 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)