python哨兵循环_Python:deadloop之非模态交互界面(模态循环)(哨兵循环)
deadloop之非模態交互界面(模態循環)(哨兵循環)
通過無限循環,條件退出,實現多模態的交互界面
主要功能1:通過鍵盤按鍵g\w\r\b進行界面顏色改變,按q退出
主要功能2:點擊界面某處生成輸入框,回車使輸入框內的文字固定在界面
當等待一個特殊的鍵入時可用,(以return為例)
while win.getkey() != 'Return':
pass
#我的菜雞簡略版本
from graphics import *
win = GraphWin('Click and Type', 500, 500)
while True:
keyout = win.checkKey()
if keyout == 'q':
break
elif keyout == 'r':
win.setBackground('pink')
elif keyout == 'w':
win.setBackground('white')
elif keyout == 'b':
win.setBackground('lightblue')
elif keyout == 'g':
win.setBackground('lightgray')
pt = win.checkMouse()
if pt:
entry = Entry(pt, 5)
entry.draw(win)
while True:
key = win.checkKey()
if key == 'Return':
break
elif key:print(key)
entry.undraw()
text = entry.getText()
Text(pt, text).draw(win)
# 教材版本(不同功能函數定義調用)
# event_loop2.py --- color-changing window
from graphics import *
def handleKey(k, win):
if k == 'r':
win.setBackground('pink')
elif k == 'w':
win.setBackground('white')
elif k == 'g':
win.setBackground('lightgray')
elif k == 'b':
win.setBackground('lightblue')
def handleClick(pt, win):
# create an Entry for user to type in
entry = Entry(pt, 10)
entry.draw(win)
# Go modal: loop until user types key
while True:
key = win.getKey()
if key == 'Return': break
else:print(key)
# undraw the entry and create and draw Text.
entry.undraw()
typed = entry.getText()
Text(pt, typed).draw(win)
# clear (ignore) ang mouse click that occurred during text entry
win.checkMouse()
def main():
win = GraphWin('Click and Type', 500, 500)
# Event loop: handle presses and mouse clicks until the user
# press the 'q' key.
while True:
key = win.checkKey()
if key == 'q': # loop exit
break
if key:
handleKey(key, win)
pt = win.checkMouse()
if pt:
handleClick(pt, win)
win.close()
main()
運行效果:
總結
以上是生活随笔為你收集整理的python哨兵循环_Python:deadloop之非模态交互界面(模态循环)(哨兵循环)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python pyside简单布局_py
- 下一篇: 信用卡冻结后多久可以销户 不同的情况需