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

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

如何在 python 中通過單擊鼠標(biāo)創(chuàng)建新的復(fù)選按鈕

如何在 python 中通過單擊鼠標(biāo)創(chuàng)建新的復(fù)選按鈕

夢(mèng)里花落0921 2023-06-06 10:28:00
我想創(chuàng)建一個(gè)程序,用戶可以通過單擊鼠標(biāo)創(chuàng)建不同的按鈕,這些按鈕應(yīng)該是獨(dú)立的。有了這個(gè)邏輯,用戶可以創(chuàng)建一個(gè)有效的復(fù)選按鈕,當(dāng)它被選中時(shí)從綠色變?yōu)榧t色。我的問題是,如果用戶再次單擊鼠標(biāo),復(fù)選按鈕會(huì)移動(dòng),而不是創(chuàng)建新的復(fù)選按鈕。任何建議如何去做?from tkinter import *root = Tk()button1 = IntVar()def color_checkbutton():  # define the colors of the checkbutton    if button1.get() == 1:        example_checkbutton.configure(bg='red')    else:        example_checkbutton.configure(bg='green')example_checkbutton = Checkbutton(root, variable=button1, textvariable=button1, command=color_checkbutton)def place_checkbutton_in_canvas(e):  # order to insert the checkbutton    xx_and = e.x    yy_and = e.y    example_checkbutton.place(x=xx_and, y=yy_and)root.bind('<Button-1>', place_checkbutton_in_canvas)root.mainloop()
查看完整描述

1 回答

?
慕容森

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

您只有一個(gè) example_checkbutton。每當(dāng)您調(diào)用該.place()方法時(shí),此按鈕都會(huì)四處移動(dòng)。


如果你想要新的,只需將它們創(chuàng)建為新的復(fù)選框小部件:


def place_checkbutton_in_canvas(e):  # order to insert the checkbutton

    if len(str(e.widget))<3: ## Don't place a new one if a checkbox was clicked

        xx_and = e.x

        yy_and = e.y

        Checkbutton(root, variable=button1, textvariable=button1, command=color_checkbutton).place(x=xx_and, y=yy_and)


這將創(chuàng)建新的復(fù)選按鈕,這些復(fù)選按鈕都鏈接到button1變量。


編輯:


如果你想要新的復(fù)選按鈕,你必須維護(hù)一個(gè) IntVar() 和 Checkbutton() 對(duì)象的列表,每次點(diǎn)擊都會(huì)變長。下面的代碼應(yīng)該可以工作。我還在創(chuàng)建時(shí)執(zhí)行顏色更改以將它們創(chuàng)建為綠色和紅色。


from tkinter import *


root = Tk()


buttons = []


class CMD: #Auxilliary function for callbacks using parameters. Syntax: CMD(function, argument1, argument2, ...)

    def __init__(s1, func, *args):

        s1.func = func

        s1.args = args

    def __call__(s1, *args):

        args = s1.args+args

        s1.func(*args)


def color_checkbutton(pos=0):  # define the colors of the checkbutton

    if buttons[pos][0].get() == 1:

        buttons[pos][2].configure(bg='red')

    else:

        buttons[pos][2].configure(bg='green')


def place_checkbutton_in_canvas(e):  # order to insert the checkbutton

    if len(str(e.widget))<3: ## Don't place a new one if a checkbox was clicked

        b = IntVar()

        pos = len(buttons)

        xx_and = e.x

        yy_and = e.y

        buttons.append([b,pos, Checkbutton(root, variable=b, textvariable=b, command=CMD(color_checkbutton,pos))])

        buttons[-1][2].place(x=xx_and, y=yy_and)

        color_checkbutton(pos)


root.bind('<Button-1>', place_checkbutton_in_canvas)


root.mainloop()


查看完整回答
反對(duì) 回復(fù) 2023-06-06
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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