返回顶部

收藏

Key action: any key pressed

更多
from Tkinter import *

class KeyDemo( Frame ):
   def __init__( self ):
      Frame.__init__( self )
      self.pack( expand = YES, fill = BOTH )
      self.master.title( 'Demonstrating Keystroke Events' )
      self.master.geometry( '350x50' )

      self.message1 = StringVar()
      self.line1 = Label( self, textvariable = self.message1 )
      self.message1.set( 'Type any key or shift' )
      self.line1.pack()

      self.message2 = StringVar()
      self.line2 = Label( self, textvariable = self.message2 )
      self.message2.set( '' )
      self.line2.pack()

      self.master.bind( '<KeyPress>', self.keyPressed )

   def keyPressed( self, event ):
      self.message1.set( 'Key pressed: ' + event.char )
      self.message2.set( 'This key is not left shift' )

def main():
   KeyDemo().mainloop()

if __name__ == '__main__':
   main()

标签:Python,Event

收藏

0人收藏

支持

0

反对

0