【解決方法】ボタンが押されているかどうかを確認するにはどうすればよいですか (Python)

プログラミングQA


だから私はボタンを持っています。 関係ありません、それはarduinoからpythonへのいくつかのirリモートです。

ボタンを押すと、「FF260」のような文字列が出力されます
問題は、保持されると、最初にそのような文字列を出力し、次に「FFFF」をスパムすることです
FF260を実行した後、FFFFを出力し続けているかどうかを検出して、保持されていることを知るにはどうすればよいですか?

私が試したこと:

解決策が思いつかない、何も試していない

解決策 2

以下のスクリプトは、ボタンの機能を理解するのに役立ちます。 これが役立つことを願っています。

import os
from tkinter import Button, Label, Tk

root = Tk()
root.geometry("400x200")

button_flag = True

lbl1 = Label(root, text="Button Actions")
lbl1.pack(pady=20)

# When the mouse is over the button and then leaves the button this action gets executed.
def on_leave(event=None):
    lbl1["text"] = "Mouse Left. The Cat did a good Job!"
    button["bg"] = "lime"

# When the Mouse  hovers over the button(enters the button) Execute this code.

def on_enter(event=None):
    lbl1["text"] = "Mouse Entered . Get the cat!"
    button["bg"] = "red"

# When the button is pressed/held execute this code
def button1pressed(event=None):
    button["text"] = "Pressed"
    lbl1["text"] = " You pressed " + str(button["bg"]) + str(button["font"])
    button_flag = False

# when you release the button execute this code. 

def button1released(event=None):
    global button_flag
    if button_flag:
        button["text"] = "Released"
        button["bg"] = "yellow"
        lbl1["text"] = " You pressed " + button["bg"] + button["font"]
        button_flag = True
    else:
        button1pressed()
        button_flag = False


button = Button(
    root,
    text="button1".title(),
    bg="yellow",
    font=("Arial Black", 20),
    command=button1released,
)
button.pack()

button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
button.bind("<ButtonRelease-1>", button1released)
button.bind("<ButtonPress-1>", button1pressed)
root.mainloop()

解決策 1

FF260 ボタンが「押された」。
このような状態で (適切なタイムアウトで) 待機します。 FFFF それから

  • それを受け取った場合、ボタンは現在「保持」されており、コードは再び待機する必要があります

一方で

  • タイムアウトになると、ボタンは「解放」されます

コメント

タイトルとURLをコピーしました