Exit form occurs anyway with or without confirmation in tray icon
-
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
import sys import os.path import ssl import urllib.error import json from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QVBoxLayout, QWidget, QSystemTrayIcon, QMenu, QAction, QMessageBox from PyQt5.QtGui import QIcon from PyQt5.QtCore import QTimer import shutil import os app_path = os.path.dirname(os.path.realpath(__file__))+'/' class MainWindow(QMainWindow): def __init__(self): super().__init__() self.file_name = 'orders' # Global variable for source folder self.source_folder = 'MQL4/Files' # Global variable for destination folder self.destination_folder = 'MQL4/Files' self.source_folder_input = QLineEdit(self) self.source_folder_input.setPlaceholderText('Source folder path') self.destination_folder_input = QLineEdit(self) self.destination_folder_input.setPlaceholderText( 'Destination folder path') self.start_button = QPushButton('Start', self) self.start_button.clicked.connect(self.start_stop) self.layout = QVBoxLayout() self.layout.addWidget(QLabel('Source Folder:')) self.layout.addWidget(self.source_folder_input) self.layout.addWidget(QLabel('Destination Folder:')) self.layout.addWidget(self.destination_folder_input) self.layout.addWidget(self.start_button) self.central_widget = QWidget() self.central_widget.setLayout(self.layout) self.setCentralWidget(self.central_widget) self.timer = QTimer() self.timer.timeout.connect(self.copy_file) self.tray_icon = QSystemTrayIcon(QIcon(app_path + 'red.png'), self) self.tray_icon.activated.connect(self.show) self.tray_menu = QMenu() self.exit_action = QAction('Exit', self) self.exit_action.triggered.connect(self.confirm_exit) self.tray_menu.addAction(self.exit_action) self.tray_icon.setContextMenu(self.tray_menu) self.tray_icon.show() self.setWindowTitle('My Application') self.setWindowIcon(QIcon(app_path + 'app_icon.png')) self.paths_file = '.paths.json' self.load_paths() def load_paths(self): if os.path.exists(self.paths_file): with open(self.paths_file, 'r') as f: paths = json.load(f) self.source_folder_input.setText(paths['source']) self.destination_folder_input.setText(paths['destination']) def save_paths(self): paths = { 'source': self.source_folder_input.text(), 'destination': self.destination_folder_input.text() } with open(self.paths_file, 'w') as f: json.dump(paths, f) def start_stop(self): if not self.source_folder_input.text() or not self.destination_folder_input.text(): QMessageBox.warning( self, 'Input Error', 'Please provide both source and destination folder paths.') return self.source_folder = os.path.join( self.source_folder_input.text(), self.source_folder) self.destination_folder = os.path.join( self.destination_folder_input.text(), self.destination_folder) if not os.path.isdir(self.source_folder) or not os.path.isdir(self.destination_folder): QMessageBox.warning(self, 'Directory Error', 'Please provide valid directory paths.') return if self.timer.isActive(): self.timer.stop() self.start_button.setText('Start') self.tray_icon.setIcon(QIcon(app_path + 'red.png')) else: self.save_paths() self.timer.start(1000) self.start_button.setText('Stop') self.tray_icon.setIcon(QIcon(app_path + 'green.png')) def copy_file(self): source_file = os.path.join(self.source_folder, self.file_name) destination_file = os.path.join( self.destination_folder, self.file_name) if not os.path.exists(source_file): with open(source_file, 'w') as f: f.write( 'Ticket,Time,Symbol,Type,OrderType,Price,Vol,TP,SL,Comment,EXPIRE\n') if not os.path.exists(destination_file): with open(destination_file, 'w') as f: f.write( 'Ticket,Time,Symbol,Type,OrderType,Price,Vol,TP,SL,Comment,EXPIRE\n') shutil.copy(source_file, destination_file) def closeEvent(self, event): if not self.timer.isActive(): reply = QMessageBox.question( self, 'Exit Confirmation', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() else: print("heeeerreeee") event.ignore() self.hide() def confirm_exit(self): self.save_paths() reply = QMessageBox.question( self, 'Exit Confirmation', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) print(format(reply)) # if reply == QMessageBox.Yes: # sys.exit() # print("Im here") # else: # print("sssss") app = QApplication(sys.argv) window = MainWindow() window.setFixedSize(556, 213) window.show() sys.exit(app.exec_())
-
@rikhtehgaran said in Exit form occurs anyway with or without confirmation in tray icon:
def confirm_exit(self):
self.save_paths()
reply = QMessageBox.question(
self, 'Exit Confirmation', 'Are you sure you want to exit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
print(format(reply))You're not closing the app here - so why should it be closed?