需要一个tkinter gui来模拟ENTER键按下

时间:2013-12-12 22:56:05

标签: python user-interface tkinter

我正在尝试使用tkinter创建一个按钮来模拟键盘上的ENTER键。我需要这个的原因是因为最终的GUI将显示在触摸屏上。没有键盘。

我的父进程正在调用一个命令,如果按下回车键,它将打开一个指示灯。每次按下输入按钮时,我都需要打开灯。被调用的命令被编程为只接受键盘上的回车键,我无法做任何改变它。

如果这是有道理的,我感谢所有帮助。

以下是我所拥有的代码正文:

import subprocess
import Tkinter

class X:

    def __init__(self):
    .....

    def __createGUI(self, ):
            self.light = Tkinter.Button(Window, text="Start", command=self.Start)
            self.light.grid()
            self.light = Tkinter.Button(Window, text="Light", command=self.Light)
            self.light.bind('<Return>') # Is this what I need tp do it?
            self.light.grid()

     def Start(self)
          command = "....." #This line of code sends a command to call a separate      program. In this program if the Enter key on the keyboard is pressed the light will turn on. This program cannot be changed
          self.p = subprocess.Popen(command, stdout = subprocess.PIPE, stderr.......)
          .....

     def Light(self)
         #I have nothing for this so far.... maybe a return "Enter" command to Start

if __name__== "__main__":
      ....
      ....

所以,我需要的是一个GUI按钮,当它被点击时,告诉Start点击了Enter。这是可以做到的吗?

1 个答案:

答案 0 :(得分:0)

output,stderror = self.p.communicate("\r")

也许你正在寻找...沟通发送命令到一个进程...不确定该程序是否正在寻找\ r,\ n或实际上正在侦听一个输入事件...如果它实际上正在侦听一个很难获得的进入事件

如果你想发送数据并继续写,我相信你可以做点什么

self.p.stdin.write("\r")

如果这不起作用,您可能需要生成一个线程来处理通信部分......

相关问题