Python,Linux/Debian/Raspberry上msvcrt.kbhit()的(有效)替代品

Python,Linux/Debian/Raspberry上msvcrt.kbhit()的(有效)替代品,python,python-3.x,linux,raspberry-pi,msvcrt,Python,Python 3.x,Linux,Raspberry Pi,Msvcrt,我试图制作一个(愚蠢的)程序,在红色和蓝色之间改变tkinter背景。我希望实现的一种方法是从终端执行python程序。我试着用Agnitic操作系统(至少是Windows和Raspberry) 问题是,在Linux上,msvcrt库没有类似的功能,所以我只能读取输入,但它会在Raspberry上阻塞屏幕(Windows版本工作正常) 这是我的密码: #!/usr/bin/env python3 import tkinter as tk import os class _tkapp(tk.F

我试图制作一个(愚蠢的)程序,在红色和蓝色之间改变tkinter背景。我希望实现的一种方法是从终端执行python程序。我试着用Agnitic操作系统(至少是Windows和Raspberry)

问题是,在Linux上,msvcrt库没有类似的功能,所以我只能读取输入,但它会在Raspberry上阻塞屏幕(Windows版本工作正常)

这是我的密码:

#!/usr/bin/env python3

import tkinter as tk
import os

class _tkapp(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

                    
    def create_widgets(self):

        self.redButton = tk.Button(self, text='Make me red', command=self.paintMeRed)
        self.redButton.pack(side='top')

        self.blueButton = tk.Button(self, text='Make me blue', command=self.paintMeBlue)
        self.blueButton.pack(side='top')

        self.quit = tk.Button(self, text='QUIT', fg='red', command=self.master.destroy)
        self.quit.pack(side='bottom')

    def paintMeRed(self):
        tk_root.configure(background='red')

    def paintMeBlue(self):
        tk_root.configure(background='blue')


def paintMeRed(event):  
    tk_root.configure(background='red')

def paintMeBlue(event): 
    tk_root.configure(background='blue')


if os.environ.get('OS','') == 'Windows_NT':
    tk_root = tk.Tk()
    tk_root.geometry("200x120+10+200") 
    tk_root.resizable(0, 0) 
    _system_ = 'Windows NT'
    import msvcrt  


elif os.environ.get('TEXTDOMAIN','') == 'Linux-PAM':
    if os.environ.get('DISPLAY','') == '':
        os.environ.__setitem__('DISPLAY', ':0.0')
    tk_root = tk.Tk()
    tk_root.geometry("200x120+10+200") 
    _system_ = 'Linux-PAM' 
    import getch  ## Linux "rewrite" for msvcrt

else:
    _logme('C','Not sure where I am being executed, bye bye...')
    exit()



tk_app = _tkapp(master=tk_root)

tk_root.bind('<Escape>', quit)
tk_root.bind('r', paintMeRed )
tk_root.bind('b', paintMeBlue )

tk_app.update()



while True:
    tk_app.update()
    kb_has_been_hit = False
    hit_Key = ''

    if _system_ == 'Linux-PAM':
        hit_Key = getch.getch()   #### THIS BLOCKs !!!!!
        kb_has_been_hit = True

    if _system_ == 'Windows NT':
        if msvcrt.kbhit():
            hit_Key = msvcrt.getwch()
            kb_has_been_hit = True

    if kb_has_been_hit:    

        if hit_Key == 'b':
            tk_root.configure(background='blue')
        elif hit_Key == 'r':
            tk_root.configure(background='red')
        elif ord(hit_Key) == 27:
            print('it has been a pleasure')
            break

print('Byyebye')
#/usr/bin/env蟒蛇3
将tkinter作为tk导入
导入操作系统
等级_tkapp(传统框架):
def uuu init uuu(self,master=None):
超级()。\uuuu初始化\uuuuu(主)
self.master=master
self.pack()
self.create_widgets()
def创建_小部件(自):
self.redButton=tk.Button(self,text='Make me red',command=self.paintMeRed)
self.redButton.pack(side='top')
self.blueButton=tk.Button(self,text='Make me blue',command=self.paintMeBlue)
self.blueButton.pack(side='top')
self.quit=tk.Button(self,text='quit',fg='red',command=self.master.destroy)
self.quit.pack(side='bottom')
def喷漆(自):
tk_root.configure(后台='red')
def paintMeBlue(自):
tk_root.configure(background='blue')
def喷漆(事件):
tk_root.configure(后台='red')
def paintMeBlue(事件):
tk_root.configure(background='blue')
如果os.environ.get('os','')='Windows\u NT':
tk_root=tk.tk()
tk_根几何(“200x120+10+200”)
tk_根。可调整大小(0,0)
_系统='Windows NT'
导入msvcrt
elif os.environ.get('TEXTDOMAIN','')='Linux-PAM':
如果os.environ.get('DISPLAY','')='':
os.environ.\uuuuuu setitem\uuuuuuuuu('显示',':0.0')
tk_root=tk.tk()
tk_根几何(“200x120+10+200”)
_系统='Linux PAM'
为msvcrt导入getch##Linux“重写”
其他:
_logme('C','不确定我在哪里被处决,再见…'))
退出()
tk\u应用程序=\u tkapp(主程序=tk\u根)
tk_root.bind(“”,退出)
tk_root.bind('r',涂漆)
tk_root.bind('b',paintMeBlue)
tk_应用程序更新()
尽管如此:
tk_应用程序更新()
kb_has_best_hit=False
点击键=“”
如果系统=Linux PAM:
点击[u Key=getch.getch()#######这个方块!!!!!
kb_have_been_hit=真
如果系统='Windows NT':
如果msvcrt.kbhit():
hit_Key=msvcrt.getwch()
kb_have_been_hit=真
如果kb\u已被\u命中:
如果点击键=='b':
tk_root.configure(background='blue')
elif hit_Key=='r':
tk_root.configure(后台='red')
elif ord(点击键)=27:
打印(“这是我的荣幸”)
打破
打印(‘再见’)
我已经搜索了几十个帖子,所有的帖子看起来都过时了,很旧了,没有一个能给我工作的选择


这在Linux/Debian/Raspberry上可能吗?

看起来您正在使用某种图形工具包。如果是这样,你就找错了地方。但我并不清楚为什么,因为您已经在使用类似于
tk\u root.bind('r',paintMeRed)
的东西。你知道它是如何工作的,或者为什么它是这样写的吗?为什么要复制它的功能?为什么您是
而不是
tkinter.mainloop()
?这么多问题。。。