Hi, I have this code. an app to copy a file from source to destination. when the app is run and the green icon shows in the tray icon. when I close the window. the window hides and the icon is still green in the tray icon. if press the exit button on the tray icon it just asks a question and then closes the app. not different you press yes or no to exit. and I can't understand what's the problem

Qt Code:
  1. import sys
  2. import os.path
  3. import ssl
  4. import urllib.error
  5. import json
  6. from PyQt5.QtGui import QIcon
  7. from PyQt5.QtCore import QTimer
  8. import shutil
  9. import os
  10.  
  11. app_path = os.path.dirname(os.path.realpath(__file__))+'/'
  12.  
  13.  
  14. class MainWindow(QMainWindow):
  15. def __init__(self):
  16. super().__init__()
  17.  
  18. self.file_name = 'orders'
  19. # Global variable for source folder
  20. self.source_folder = 'MQL4/Files'
  21. # Global variable for destination folder
  22. self.destination_folder = 'MQL4/Files'
  23.  
  24. self.source_folder_input = QLineEdit(self)
  25. self.source_folder_input.setPlaceholderText('Source folder path')
  26.  
  27. self.destination_folder_input = QLineEdit(self)
  28. self.destination_folder_input.setPlaceholderText(
  29. 'Destination folder path')
  30.  
  31. self.start_button = QPushButton('Start', self)
  32. self.start_button.clicked.connect(self.start_stop)
  33.  
  34. self.layout = QVBoxLayout()
  35. self.layout.addWidget(QLabel('Source Folder:'))
  36. self.layout.addWidget(self.source_folder_input)
  37. self.layout.addWidget(QLabel('Destination Folder:'))
  38. self.layout.addWidget(self.destination_folder_input)
  39. self.layout.addWidget(self.start_button)
  40.  
  41. self.central_widget = QWidget()
  42. self.central_widget.setLayout(self.layout)
  43. self.setCentralWidget(self.central_widget)
  44.  
  45. self.timer = QTimer()
  46. self.timer.timeout.connect(self.copy_file)
  47.  
  48. self.tray_icon = QSystemTrayIcon(QIcon(app_path + 'red.png'), self)
  49. self.tray_icon.activated.connect(self.show)
  50. self.tray_menu = QMenu()
  51. self.exit_action = QAction('Exit', self)
  52. self.exit_action.triggered.connect(self.confirm_exit)
  53. self.tray_menu.addAction(self.exit_action)
  54. self.tray_icon.setContextMenu(self.tray_menu)
  55. self.tray_icon.show()
  56.  
  57. self.setWindowTitle('My Application')
  58. self.setWindowIcon(QIcon(app_path + 'app_icon.png'))
  59.  
  60. self.paths_file = '.paths.json'
  61. self.load_paths()
  62. def load_paths(self):
  63. if os.path.exists(self.paths_file):
  64. with open(self.paths_file, 'r') as f:
  65. paths = json.load(f)
  66. self.source_folder_input.setText(paths['source'])
  67. self.destination_folder_input.setText(paths['destination'])
  68.  
  69. def save_paths(self):
  70. paths = {
  71. 'source': self.source_folder_input.text(),
  72. 'destination': self.destination_folder_input.text()
  73. }
  74. with open(self.paths_file, 'w') as f:
  75. json.dump(paths, f)
  76.  
  77. def start_stop(self):
  78. if not self.source_folder_input.text() or not self.destination_folder_input.text():
  79. QMessageBox.warning(
  80. self, 'Input Error', 'Please provide both source and destination folder paths.')
  81. return
  82.  
  83. self.source_folder = os.path.join(
  84. self.source_folder_input.text(), self.source_folder)
  85. self.destination_folder = os.path.join(
  86. self.destination_folder_input.text(), self.destination_folder)
  87.  
  88. if not os.path.isdir(self.source_folder) or not os.path.isdir(self.destination_folder):
  89. QMessageBox.warning(self, 'Directory Error',
  90. 'Please provide valid directory paths.')
  91. return
  92.  
  93. if self.timer.isActive():
  94. self.timer.stop()
  95. self.start_button.setText('Start')
  96. self.tray_icon.setIcon(QIcon(app_path + 'red.png'))
  97. else:
  98. self.save_paths()
  99. self.timer.start(1000)
  100. self.start_button.setText('Stop')
  101. self.tray_icon.setIcon(QIcon(app_path + 'green.png'))
  102.  
  103. def copy_file(self):
  104. source_file = os.path.join(self.source_folder, self.file_name)
  105. destination_file = os.path.join(
  106. self.destination_folder, self.file_name)
  107.  
  108. if not os.path.exists(source_file):
  109. with open(source_file, 'w') as f:
  110. f.write(
  111. 'Ticket,Time,Symbol,Type,OrderType,Price,Vol,TP,SL,Comment,EXPIRE\n')
  112.  
  113. if not os.path.exists(destination_file):
  114. with open(destination_file, 'w') as f:
  115. f.write(
  116. 'Ticket,Time,Symbol,Type,OrderType,Price,Vol,TP,SL,Comment,EXPIRE\n')
  117.  
  118. shutil.copy(source_file, destination_file)
  119.  
  120. def closeEvent(self, event):
  121. if not self.timer.isActive():
  122. reply = QMessageBox.question(
  123. self, 'Exit Confirmation', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  124. if reply == QMessageBox.Yes:
  125. event.accept()
  126. else:
  127. event.ignore()
  128. else:
  129. print("heeeerreeee")
  130. event.ignore()
  131. self.hide()
  132.  
  133. def confirm_exit(self):
  134. self.save_paths()
  135. reply = QMessageBox.question(
  136. self, 'Exit Confirmation', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  137. print(format(reply))
  138. # if reply == QMessageBox.Yes:
  139. # sys.exit()
  140. # print("Im here")
  141. # else:
  142. # print("sssss")
  143.  
  144. app = QApplication(sys.argv)
  145. window = MainWindow()
  146. window.setFixedSize(556, 213)
  147. window.show()
  148. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode