2012-08-17 84 views
0

考慮下面的代碼:掛在連接到顯示

#!/usr/bin/env python 

from Xlib.display import Display 
import os 

def main(): 
     disp = Display() # connect to display 
     while True: 
       print("1") 
       event = disp.next_event() 
       print("2") 
       if event.type == Xlib.protocol.event.KeyPress: 
         print("keypress!") 

if __name__ == '__main__': 
     main() 

該代碼輸出1但不輸出2。它似乎掛在顯示器連接上。爲什麼會這樣?謝謝。

回答

1

方法next_event()是一個阻塞方法,請參見documentation

返回在事件隊列中的下一個事件。如果事件隊列爲空,則阻止 ,直到從網絡讀取事件並返回該事件。

如果它被阻止是因爲還沒有事件到達。

+0

謝謝塔蘭圖拉! – dotancohen 2012-08-17 16:41:14