SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tut9.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. # TQt tutorial 9.
  3. import sys
  4. from python_tqt.qt import *
  5. class LCDRange(TQVBox):
  6. def __init__(self,parent=None,name=None):
  7. TQVBox.__init__(self,parent,name)
  8. lcd = TQLCDNumber(2,self,'lcd')
  9. self.slider = TQSlider(TQt.Horizontal,self,'slider')
  10. self.slider.setRange(0,99)
  11. self.slider.setValue(0)
  12. self.connect(self.slider,SIGNAL('valueChanged(int)'),lcd,SLOT('display(int)'))
  13. self.connect(self.slider,SIGNAL('valueChanged(int)'),self,PYSIGNAL('valueChanged(int)'))
  14. self.setFocusProxy(self.slider)
  15. def value(self):
  16. return self.slider.value()
  17. def setValue(self,value):
  18. self.slider.setValue(value)
  19. def setRange(self,minVal,maxVal):
  20. if minVal < 0 or maxVal > 99 or minVal > maxVal:
  21. raise ValueError, 'LCDRange.setRange(): invalid range'
  22. self.slider.setRange(minVal,maxVal)
  23. class CannonField(TQWidget):
  24. def __init__(self,parent=None,name=None):
  25. TQWidget.__init__(self,parent,name)
  26. self.ang = 45
  27. self.setPalette(TQPalette(TQColor(250,250,200)))
  28. def angle(self):
  29. return self.ang
  30. def setAngle(self,degrees):
  31. if degrees < 5:
  32. degrees = 5
  33. if degrees > 70:
  34. degrees = 70
  35. if self.ang == degrees:
  36. return
  37. self.ang = degrees
  38. self.repaint()
  39. self.emit(PYSIGNAL('angleChanged(int)'),(self.ang,))
  40. def paintEvent(self,ev):
  41. p = TQPainter(self)
  42. p.setBrush(TQt.blue)
  43. p.setPen(TQt.NoPen)
  44. p.translate(0,self.rect().bottom())
  45. p.drawPie(TQRect(-35,-35,70,70),0,90 * 16)
  46. p.rotate(-self.ang)
  47. p.drawRect(TQRect(33,-4,15,8))
  48. def sizePolicy(self):
  49. return TQSizePolicy(TQSizePolicy.Expanding,TQSizePolicy.Expanding)
  50. class MyWidget(TQWidget):
  51. def __init__(self,parent=None,name=None):
  52. TQWidget.__init__(self,parent,name)
  53. quit = TQPushButton('&Quit',self,'quit')
  54. quit.setFont(TQFont('Times',18,TQFont.Bold))
  55. self.connect(quit,SIGNAL('clicked()'),tqApp,SLOT('quit()'))
  56. self.angle = LCDRange(self,'angle')
  57. self.angle.setRange(5,70)
  58. self.cannonField = CannonField(self,'cannonField')
  59. self.connect(self.angle,PYSIGNAL('valueChanged(int)'),self.cannonField.setAngle)
  60. self.connect(self.cannonField,PYSIGNAL('angleChanged(int)'),self.angle.setValue)
  61. grid = TQGridLayout(self,2,2,10)
  62. grid.addWidget(quit,0,0)
  63. grid.addWidget(self.angle,1,0,TQt.AlignTop)
  64. grid.addWidget(self.cannonField,1,1)
  65. grid.setColStretch(1,10)
  66. self.angle.setValue(60)
  67. self.angle.setFocus()
  68. TQApplication.setColorSpec(TQApplication.CustomColor)
  69. a = TQApplication(sys.argv)
  70. w = MyWidget()
  71. w.setGeometry(100,100,500,355)
  72. a.setMainWidget(w)
  73. w.show()
  74. a.exec_loop()