使用Tkinter检测密钥while(1)

时间:2017-07-18 12:26:38

标签: python tkinter

我想通过功能Tkinter检测键盘的某些键。 这里是我的脚本,具有此功能的基本实现。

from pylab import *
from rtlsdr import *
from bluetooth import *

import sys
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import Tkinter as tk

sdr=RtlSdr()

#configure device
sdr.center_freq=double(sys.argv[1]) # centrale frequency
sdr.gain=double(sys.argv[2]) #gain
sdr.sample_rate=double(sys.argv[3]) # sample rate

#configure PSD
NFFT=int(sys.argv[4]) # nb points

# Bluetooth connexion
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock, client_info=server_sock.accept()

#configure tk
root= tk.Tk()
root.geometry('300x200')
text=tk.Text(root, background='black', foreground='white', font=('Comic Sans MS',12))
text.pack()

def on_key_press(event):
     if event.char=='a':
           sdr.center_freq= sdr.center_freq+0.1e6
     if event.char=='z':
           sdr.center_freq= sdr.center_freq-0.1e6

while (1):

    samples=sdr.read_samples(256*1024)
        result=psd(samples,NFFT,Fs=sdr.sample_rate/1e6,Fc=sdr.center_freq*1e6/1e6)

        tab_freq=(result[1]/1e6)
        value_freq=str(tab_freq)[1:-1]
        value_list=[format(float(v), ".5f") for v in value_freq.split()]
        value_freq2= "\n".join(value_list)

        tab_pxx=result[0]
        value_pxx=str(tab_pxx)[1:-1]
        value_list2=[format(float(v), ".7f") for v in value_pxx.split()]
        value_pxx2= "\n".join(value_list2)

        client_sock.send(value_freq2+'\n'+'\n'.join(value_pxx2.split()))

        root.bind('<KeyPress>', on_key_press)
        root.mainloop()

实际上我可以检测到键盘的某些键。 问题是:这个窗口会阻塞循环,所以我的sdr无法获得新的样本。 我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用tkinter的after方法来安排函数调用。该函数将执行您在while循环中执行的所有任务,然后使用after计划对自身的另一个调用;有效地创建了一个while循环:

sdr=RtlSdr()

#configure device
sdr.center_freq=double(sys.argv[1]) # centrale frequency
sdr.gain=double(sys.argv[2]) #gain
sdr.sample_rate=double(sys.argv[3]) # sample rate

#configure PSD
NFFT=int(sys.argv[4]) # nb points

# Bluetooth connection
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock, client_info=server_sock.accept()

def on_key_press(event):
    if event.char=='a':
        sdr.center_freq= sdr.center_freq+0.1e6
    if event.char=='z':
        sdr.center_freq= sdr.center_freq-0.1e6

def get_samples():
    samples=sdr.read_samples(256*1024)
    result=psd(samples,NFFT,Fs=sdr.sample_rate/1e6,Fc=sdr.center_freq*1e6/1e6)

    tab_freq=(result[1]/1e6)
    value_freq=str(tab_freq)[1:-1]
    value_list=[format(float(v), ".5f") for v in value_freq.split()]
    value_freq2= "\n".join(value_list)

    tab_pxx=result[0]
    value_pxx=str(tab_pxx)[1:-1]
    value_list2=[format(float(v), ".7f") for v in value_pxx.split()]
    value_pxx2= "\n".join(value_list2)

    client_sock.send(value_freq2+'\n'+'\n'.join(value_pxx2.split()))

    root.after(1, get_samples)

#configure tk
root = tk.Tk()
root.geometry('300x200')
root.bind('<KeyPress>', on_key_press)

text = tk.Text(root, bg='black', fg='white', font=('Comic Sans MS',12))
text.pack()

root.after(1, get_samples)
root.mainloop()

More about after

我还建议您使用OOP方法开始构建应用程序。对于这个应用程序:

class App(tk.Tk):
    """Create the main application GUI."""

    def __init__(self):
        super().__init__()

        self.geometry('300x200')
        self.bind('<KeyPress>', on_key_press)

        self.text = tk.Text(root, bg='black', fg='white', font=('Comic Sans MS',12))
        self.text.pack()

        self.config()
        self.connect()

    def config(self):
        # configure device
        # configure PSD

    def connect(self):
        # bluetooth connection

    def onKey(self):
        # on key press

    def get_samples(self):
        # get samples

        self.after(1, self.get_samples)

    def run(self):
        self.after(1, self.get_samples)
        self.mainloop()