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

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

在函數(shù)中無法禁用 tkiner 按鈕

在函數(shù)中無法禁用 tkiner 按鈕

慕桂英546537 2023-06-13 10:39:45
基本上,我目前正在編寫一個(gè) python 程序(技術(shù)上它是一個(gè)游戲),其中某個(gè)(TKinter Widget)按鈕只能每 1 秒單擊一次。這是我的意思的一個(gè)例子:import timefrom tkinter import *def button_click():    button["state"] = DISABLED    print("button clicked! Please wait 1 second...")    time.sleep(1)    button["state"] = NORMALroot = Tk()button = Button(root, text="Click Me!", command=button_click)button.pack() #Please Dont Tell Me Not To Use Pack() ; I Use Place()所以無論如何,例如,當(dāng)運(yùn)行這個(gè)程序時(shí),如果我一直點(diǎn)擊按鈕,它會(huì)每秒增加 1 個(gè)計(jì)數(shù)。相反,我希望它不計(jì)算第一次點(diǎn)擊和之后 1 秒之間發(fā)生的所有點(diǎn)擊。
查看完整描述

2 回答

?
ibeautiful

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

這是因?yàn)楫?dāng)按鈕被禁用時(shí) tkinter 沒有控制,所以它沒有更新。例如,您需要button.update()在禁用后調(diào)用以強(qiáng)制更新:


def button_click():

    button["state"] = DISABLED

    button.update() # force the update

    print("button clicked! Please wait 1 second...")

    time.sleep(1)

    button["state"] = NORMAL

但是,最好使用after()而不是time.sleep():


def button_click():

    button["state"] = DISABLED

    print("button clicked! Please wait 1 second...")

    # enable the button after one second

    button.after(1000, lambda: button.config(state='normal'))


查看完整回答
反對(duì) 回復(fù) 2023-06-13
?
GCT1015

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

也許您忘記了代碼末尾的“root.mainloop”。


import time

from tkinter import *

def button_click():

    button["state"] = DISABLED

    print("button clicked! Please wait 1 second...")

    time.sleep(1)

    button["state"] = NORMAL


root = Tk()


button = Button(root, text="Click Me!", command=button_click)


button.pack()


root.mainloop()

這對(duì)我有用。您只能每 1 秒按下一次按鈕。


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

添加回答

舉報(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)