按tkinter

时间:2017-10-25 18:13:08

标签: python python-3.x user-interface tkinter

我目前正在尝试使用两个Raspberry Pi创建一个报警系统,通过连接到其中一个按钮的按钮(通过面包板)模拟发出警报的触发器。秒。当按下此按钮时(因此警报被激活),用户按下使用tkinter制作的GUI中的按钮,有30秒的时间来关闭警报。如果在30秒内没有按下按钮,则会向不同的RPI发送TCP。

所以现在的问题是:如何创建一个带有tkinter的GUI,如果用户按下按钮,停止pi发送TCP' STOP ALARM'在GUI上。

这就是我的尝试:

(我没有包含buttonpressed& sendtcp功能,因为它们非常长。)

import import RPi.GPIO as GPIO
from tkinter import *
import socket
import sys
import time
def alarmstopped()
    return True
count = 0
while True:
    if buttonPressed() == True: #Button pressed
        while True:
            if alarmStoped() == True: #Stops 30sec count
                break
                count = 0
            elif count >=30:
                sendTCP() #Sends TCP to other RPI
                time.sleep(0.2)
                break
            else:
                count += 1
                time.sleep(1)
                print(count)
                continue
    else:
        continue
root = Tk()
button = Button(master=root, cursor='hand2', text='stop alarm', command=alarmStoped)
button.pack()

Pastebin

希望你们能帮助我,如果我犯了错误,可以随意纠正我的英语;)

1 个答案:

答案 0 :(得分:0)

根本不需要while循环。相反,要使用after启动闹钟,并停止使用after_cancelafter将安排将来运行的功能(例如:打开闹钟),after_cancel将停止该作业的运行。

这是一个简单的例子,通过在10秒内设置闹钟来说明该技术:

import tkinter as tk

def on_start_alarm():
    global alarm_id
    alarm_id = root.after(10000, start_alarm)
    alarm.configure(text="Alarm starting soon...")

def on_stop_alarm():
    global alarm_id
    alarm.configure(text="")
    root.after_cancel(alarm_id)

def start_alarm():
    alarm.configure(text='Alarm! Alarm!', foreground="red")

root = tk.Tk()

alarm = tk.Label(root, text="", width=20)
start = tk.Button(root, text="Start", command=on_start_alarm)
stop = tk.Button(root, text="Stop", command=on_stop_alarm)

alarm.pack(side="top", fill="both", expand=True)
start.pack(side="top")
stop.pack(side="top")

root.mainloop()
相关问题