A small exercise for a friend. Contain basic python operations, global variables, gtk interactions and a Glade windows UI file.
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.

main.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import gi
  2. gi.require_version("Gtk", "3.0")
  3. from gi.repository import Gtk
  4. iTotal = None
  5. iOperation = None
  6. sTmpString = None
  7. iJustDone = 0
  8. class MyClass:
  9. def addInt(iInt):
  10. global sTmpString
  11. global builder
  12. if sTmpString == None:
  13. sTmpString = str(iInt)
  14. else:
  15. sTmpString = sTmpString + str(iInt)
  16. lblLabel = builder.get_object("lblResult")
  17. lblLabel.set_markup(sTmpString);
  18. return None
  19. def doOperation(iOp):
  20. global sTmpString
  21. global iTotal
  22. global iOperation
  23. global builder
  24. global iJustDone
  25. if iJustDone == 1:
  26. sTmpString = None
  27. iJustDone = 0
  28. else:
  29. if sTmpString is not None:
  30. iOperation = iOp
  31. if iTotal == None:
  32. iTotal = int(sTmpString)
  33. else:
  34. if iOp == 0:
  35. iTotal = iTotal + int(sTmpString)
  36. if iOp == 1:
  37. btnItem.set_markup("-")
  38. iTotal = iTotal - int(sTmpString)
  39. if iOp == 2:
  40. btnItem.set_markup("*")
  41. iTotal = iTotal * int(sTmpString)
  42. if iOp == 3:
  43. btnItem.set_markup("/")
  44. iTotal = iTotal / int(sTmpString)
  45. sTmpString = None
  46. lblLabel = builder.get_object("lblResult")
  47. lblLabel.set_markup(str(iTotal))
  48. class Handler:
  49. def onDestroy(self, *args):
  50. Gtk.main_quit()
  51. def btnDivide_clicked_cb(self, button):
  52. global builder
  53. btnItem = builder.get_object("lblOperator")
  54. btnItem.set_markup("/")
  55. MyClass.doOperation(3)
  56. def btnMultiple_clicked_cb(self, button):
  57. global builder
  58. btnItem = builder.get_object("lblOperator")
  59. btnItem.set_markup("*")
  60. MyClass.doOperation(2)
  61. def btnMinus_clicked_cb(self, button):
  62. global builder
  63. btnItem = builder.get_object("lblOperator")
  64. btnItem.set_markup("-")
  65. MyClass.doOperation(1)
  66. def btnPlus_clicked_cb(self, button):
  67. global builder
  68. btnItem = builder.get_object("lblOperator")
  69. btnItem.set_markup("+")
  70. MyClass.doOperation(0)
  71. def btnDo_clicked_cb(self, button):
  72. global sTmpString
  73. global iTotal
  74. global iOperation
  75. global builder
  76. global iJustDone
  77. lblLabel = builder.get_object("lblResult")
  78. if iTotal is not None:
  79. if sTmpString is None:
  80. iInput = iTotal
  81. sTmpString = str(iInput);
  82. else:
  83. iInput = int(sTmpString)
  84. if iOperation is not None:
  85. iJustDone = 1;
  86. if iOperation == 0:
  87. iTotal = int(iTotal) + int(iInput)
  88. if iOperation == 1:
  89. iTotal = int(iTotal) - int(iInput)
  90. if iOperation == 2:
  91. iTotal = int(iTotal) * int(iInput)
  92. if iOperation == 3:
  93. iTotal = int(iTotal) / int(iInput)
  94. lblLabel.set_markup(str(iTotal))
  95. else:
  96. iTotal = None
  97. sTmpString = None
  98. iOperation = None
  99. iJustDone = 0
  100. lblLabel.set_markup("Not enough data")
  101. def btnReset_clicked_cb(self, button):
  102. global sTmpString
  103. global iTotal
  104. global iOperation
  105. global iJustDone
  106. sTmpString = None
  107. iTotal = None
  108. iOperation = None
  109. iJustDone = 0
  110. global builder
  111. lblLabel = builder.get_object("lblResult")
  112. lblLabel.set_markup("0")
  113. btnItem = builder.get_object("lblOperator")
  114. btnItem.set_markup("#")
  115. def btn0_clicked_cb(self, button):
  116. MyClass.addInt(0)
  117. def btn1_clicked_cb(self, button):
  118. MyClass.addInt(1)
  119. def btn2_clicked_cb(self, button):
  120. MyClass.addInt(2)
  121. def btn3_clicked_cb(self, button):
  122. MyClass.addInt(3)
  123. def btn4_clicked_cb(self, button):
  124. MyClass.addInt(4)
  125. def btn5_clicked_cb(self, button):
  126. MyClass.addInt(5)
  127. def btn6_clicked_cb(self, button):
  128. MyClass.addInt(6)
  129. def btn7_clicked_cb(self, button):
  130. MyClass.addInt(7)
  131. def btn8_clicked_cb(self, button):
  132. MyClass.addInt(8)
  133. def btn9_clicked_cb(self, button):
  134. MyClass.addInt(9)
  135. builder = Gtk.Builder()
  136. builder.add_from_file("src/window.ui")
  137. builder.connect_signals(Handler())
  138. window = builder.get_object("window1")
  139. window.show_all()
  140. Gtk.main()