簡體   English   中英

我只想在 kivy 中一個接一個地單擊兩個按鈕時打印文本

[英]I want to print a text only if the two buttons are clicked one after the other in kivy

def build(self): 
   layout=FloatLayout()
 
    # use a (r, g, b, a) tuple 

    btn1 = Button(text ="Push Me !",
    background_color =(1, 1, 1,1)
    size =(32, 32), 
    size_hint =(.2, .2), 
    pos =(300, 250)) 

    btn2  = Button(text ="click Me !",
    background_color =(1, 0, 1,1)
    size =(32, 32), 
    size_hint =(.2, .2), 
    pos =(100, 250)) 

    layout.add_widget(btn1)
    layout.add_widget(btn2)

    # I need a function here to print a text only if the two buttons are clicked one after another.

    return  layout

例如,您可以在單擊第一個按鈕時更新變量值。 然后,當單擊第二個按鈕時,您檢查此變量的值,如果它具有正確的值,則打印文本,當然,您重置變量。

這是一個簡單的代碼:

var_click = 0
def click1(*events):
     global var_click
     var_click = 1

def click2(*events):
     global var_click
     if var_click == 1:
         var_click = 0
         print("Some text")

然后你只需將這兩個函數分別關聯到btn1btn2command參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM