class SampleWindow(QMainWindow):
 """ Our main window class
 """
 def __init__(self,fileName=None):
    """ Constructor Function
    """
    # QWidget.__init__(self)
    # self.setWindowTitle("Icon Sample")
    # self.setGeometry(300, 300, 200, 150)
    QMainWindow.__init__(self)
    self.setWindowTitle("Icon Sample")
    self.setGeometry(300, 300, 200, 150)
    QToolTip.setFont(QFont("Decorative", 8, QFont.Bold))
    self.setToolTip('Our Main Window')
    self.icon='C:\Users\Hamed\Documents\soheil sites image\imageedit__9411602959.gif'
    self.textEdit = QTextEdit()
    self.setCentralWidget(self.textEdit)
    self.fileName = None
    self.filters = "Text files (*.txt)"

    openFile = QAction(QIcon('open.png'), 'Open', self)
    openFile.setShortcut('Ctrl+O')
    openFile.setStatusTip('Open new File')
    openFile.triggered.connect(self.showDialog)
    menubar = self.menuBar()
    # fileMenu = menubar.addMenu('&File')
    # fileMenu.addAction(openFile)
    self.setGeometry(300, 300, 350, 300)
    self.setWindowTitle('Example - File Dialog')

    # self.myNameLE = QLineEdit(self)
    # self.myAgeLE = QLineEdit(self)
    # self.myChoiceLE = QLineEdit(self)

    self.statusLabel = QLabel('Showing Progress')
    self.progressBar = QProgressBar()
    self.progressBar.setMinimum(0)
    self.progressBar.setMaximum(100)
##################@@@@@@@@@@@@@@2
    self.threads = []

    self.addWorker(MyWorkerThread(1))
    self.addWorker(MyWorkerThread(2))
#######################@@@@@@@@@@@@@
    self.show()
##########################@@@@@@@@@@
 def addWorker(self, worker):
        worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
        # connect the finished signal to method so that we are notified
        worker.finished.connect(self.workersFinished)
        self.threads.append(worker)

 def startWorkers(self):
        for worker in self.threads:
            worker.start()
            # no wait, no finished. you start the threads and leave.

 def workersFinished(self):
        if all(worker.isFinished() for worker in self.threads):
            # wait until all the threads finished
            QtCore.QCoreApplication.instance().quit()

 @QtCore.Slot(str)
 def printMessage(self, text):
        sys.stdout.write(text+'\n')
        sys.stdout.flush()
################################

 def openAbout(self):
    aboutDialog = QtGui.QDialog(self)
    # aboutUi = about.About_Dialog()
    # aboutUi.setupUi(aboutDialog)
    aboutDialog.show()

 def newwindow(self):
        # w = W1()
        # w.show()
        # self.hide()
        # form = Form()
        # form.show()

        w2 = chooseoption.Form1(self)
        w2.show()
        import webbrowser
        your_swf_url='E:\soheil\web_site_root\ieee\all_functions\linux server\python GUI\Double_angle_off.swf'
        webbrowser.open(your_swf_url)
        # self.wid = QWidget()
        # self.wid.resize(250, 150)
        # self.wid.setWindowTitle('NewWindow')
        # self.wid.show()
        # self.actionAbout.triggered.connect(self.openAbout)
 def retranslateUi(self, Dialog):
    Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    self.aboutLbl.setText(QtGui.QApplication.translate("Dialog", "Mailer version 0.0.1 by .....", None, QtGui.QApplication.UnicodeUTF8))

 def showNameDialog(self):
    text, ok = QInputDialog.getText(self, 'Input Text Dialog',
    'Enter your name:')
    if ok:
        self.myNameLE.setText(str(text))

 def showDialog(self):
    fileName, _ = QFileDialog.getOpenFileName(self, "Open Text Files", "c:/", "Text files(*.txt)")
    try:
        contents = open(fileName, 'r')
        with contents:
            data = contents.read()
            self.textEdit.setText(data)
    except:
        pass

 def CreateStatusBar(self):
    """ Function to create the status bar
    """
    self.myStatusBar = QStatusBar()
    self.progressBar.setValue(10)
    self.myStatusBar.addWidget(self.statusLabel, 1)
    self.myStatusBar.addWidget(self.progressBar, 2)
    self.setStatusBar(self.myStatusBar)
 def ShowProgress(self):
    """ Function to show progress
    """
    import time
    while(self.progressBar.value() < self.progressBar.maximum()):
        self.progressBar.setValue(self.progressBar.value() + 10)
        # time.sleep(1)
    self.statusLabel.setText('Ready')
 def SetupComponents(self):
    """ Function to setup status bar, central widget, menu bar
    """
    self.myStatusBar = QStatusBar()
    self.setStatusBar(self.myStatusBar)
    self.myStatusBar.showMessage('Ready', 10000)
    self.textEdit = QTextEdit()
    self.setCentralWidget(self.textEdit)
    # self.CreateActions()
    # self.CreateMenus()
    # self.fileMenu.addAction(self.newAction)
    # self.fileMenu.addSeparator()
    # self.fileMenu.addAction(self.exitAction)
    # self.editMenu.addAction(self.copyAction)
    # self.fileMenu.addSeparator()
    # self.editMenu.addAction(self.pasteAction)
    # self.helpMenu.addAction(self.aboutAction)



    self.myStatusBar = QStatusBar()
    self.setStatusBar(self.myStatusBar)
    self.myStatusBar.showMessage('Ready', 10000)
    self.CreateActions()
    self.CreateMenus()
    self.CreateToolBar()
    self.fileMenu.addAction(self.newAction)
    self.fileMenu.addAction(self.openAction)
    self.fileMenu.addAction(self.saveAction)
    self.fileMenu.addSeparator()
    self.fileMenu.addAction(self.exitAction)
    self.editMenu.addAction(self.cutAction)
    self.editMenu.addAction(self.copyAction)
    self.editMenu.addAction(self.pasteAction)
    self.editMenu.addSeparator()
    self.editMenu.addAction(self.undoAction)
    self.editMenu.addAction(self.redoAction)
    self.editMenu.addAction(self.ss_image)

    self.editMenu.addSeparator()
    self.editMenu.addAction(self.selectAllAction)
    self.formatMenu.addAction(self.fontAction)
    self.helpMenu.addAction(self.aboutAction)
    self.helpMenu.addSeparator()
    self.helpMenu.addAction(self.aboutQtAction)
    self.mainToolBar.addAction(self.newAction)
    self.mainToolBar.addAction(self.openAction)
    self.mainToolBar.addAction(self.saveAction)
    self.mainToolBar.addSeparator()
    self.mainToolBar.addAction(self.cutAction)
    self.mainToolBar.addAction(self.copyAction)
    self.mainToolBar.addAction(self.pasteAction)
    self.mainToolBar.addSeparator()
    self.mainToolBar.addAction(self.undoAction)
    self.mainToolBar.addAction(self.redoAction)


 def openFile(self):
    self.fileName, self.filterName =QFileDialog.getOpenFileName(self)
    try:
        self.textEdit.setText(open(self.fileName).read())
    except:
        pass
    # Slots called when the menu actions are triggered
 def newFile(self):
    self.textEdit.setText('')
 def exitFile(self):
    self.close()
 def aboutHelp(self):
    QMessageBox.about(self, "About Simple Text Editor",
    "This example demonstrates the use "
    "of Menu Bar")

 def fontChange(self):
    (font, ok) = QFontDialog.getFont(QFont("Helvetica [Cronyx]", 10), self)
    if ok:
        self.textEdit.setCurrentFont(font)

 def saveFile(self):
    if self.fileName == None or self.fileName == '':
        self.fileName, self.filterName = QFileDialog.getSaveFileName(self, \
        filter=self.filters)
    if(self.fileName != ''):
        file = open(self.fileName, 'w')
        file.write(self.textEdit.toPlainText())
        self.statusBar().showMessage("File saved", 2000)
 def image_ss(self):
    from PySide import QtGui, QtCore

    import wxpython_flash_Simple_working
    hbox = QtGui.QHBoxLayout(self)
    pixmap = QtGui.QPixmap('C:\Users\Hamed\Pictures\LED\led.jpg')

    lbl = QtGui.QLabel(self)
    lbl.setPixmap(pixmap)

    hbox.addWidget(lbl)
    self.setLayout(hbox)

    self.setGeometry(300, 300, 280, 170)
    self.setWindowTitle('Red Rock')

    self.show()


    # from PIL import Image
    # from PySide.QtGui import QImage, QImageReader, QLabel, QPixmap, QApplication
    #
    # im = Image.open('C:\Users\Hamed\Documents\soheil sites image\imageedit__9411602959.gif')
    # data = im.tostring('raw')
    #
    ##app = QApplication([])
    ## image = QImage(data);
    # image = QImage(data, im.size[0], im.size[1], QImage.Format_ARGB32)
    # pix = QPixmap.fromImage(image)
    # lbl = QLabel()
    # lbl.setPixmap(pix)
    # lbl.show()
 def image_ss_main(self):
    from PySide import QtGui, QtCore
    hbox = QtGui.QHBoxLayout(self)
    pixmap = QtGui.QPixmap('C:\Users\Hamed\Pictures\LED\led.jpg')

    lbl = QtGui.QLabel(self)
    lbl.setPixmap(pixmap)

    hbox.addWidget(lbl)
    self.setLayout(hbox)

    self.setGeometry(300, 300, 280, 170)
    self.setWindowTitle('Red Rock')

    self.show()


    # from PIL import Image
    # from PySide.QtGui import QImage, QImageReader, QLabel, QPixmap, QApplication
    #
    # im = Image.open('C:\Users\Hamed\Documents\soheil sites image\imageedit__9411602959.gif')
    # data = im.tostring('raw')
    #
    ##app = QApplication([])
    ## image = QImage(data);
    # image = QImage(data, im.size[0], im.size[1], QImage.Format_ARGB32)
    # pix = QPixmap.fromImage(image)
    # lbl = QLabel()
    # lbl.setPixmap(pix)
    # lbl.show(
 def CreateActions(self):
    """ Function to create actions for menus
    """
    self.newAction = QAction( QIcon('new.png'), '&New',
    self, shortcut=QKeySequence.New,
    statusTip="Create a New File",
    triggered=self.newFile)
    self.exitAction = QAction( QIcon(self.icon), 'E&xit',
    self, shortcut="Ctrl+Q",
    statusTip="Exit the Application",
    triggered=self.exitFile)
    self.copyAction = QAction( QIcon('copy.png'), 'C&opy',
    self, shortcut="Ctrl+C",
    statusTip="Copy",
    triggered=self.textEdit.copy)
    self.pasteAction = QAction( QIcon('paste.png'), '&Paste',
    self, shortcut="Ctrl+V",
    statusTip="Paste",
    triggered=self.textEdit.paste)
    self.aboutAction = QAction( QIcon('about.png'), 'A&bout',
    self, statusTip="Displays info about text editor",
    triggered=self.aboutHelp)

    self.openAction = QAction( QIcon('open.png'), 'O&pen',
    self, shortcut=QKeySequence.Open,
    statusTip="Open an existing file",
    triggered=self.openFile)

    self.saveAction = QAction( QIcon('save.png'), '&Save',
    self, shortcut=QKeySequence.Save,
    statusTip="Save the current file to disk",
    triggered=self.saveFile)

    self.cutAction = QAction( QIcon('cut.png'), 'C&ut',
    self, shortcut=QKeySequence.Cut,
    statusTip="Cut the current selection to clipboard",
    triggered=self.textEdit.cut)

    self.undoAction = QAction( QIcon('undo.png'),'Undo', self,
    shortcut=QKeySequence.Undo,
    statusTip="Undo previous action",
    triggered=self.textEdit.undo)

    self.redoAction = QAction( QIcon('redo.png'),'Redo', self,
    shortcut=QKeySequence.Redo,
    statusTip="Redo previous action",
    triggered=self.textEdit.redo)

    self.selectAllAction = QAction( QIcon('selectAll.png'),
    'Select All',
    self, statusTip="Select All",
    triggered=self.textEdit.selectAll)

    self.fontAction = QAction( 'F&ont', self,
    statusTip = "Modify font properties",
    triggered = self.fontChange)

    self.aboutAction = QAction( QIcon('about.png'), 'A&bout',
    self, statusTip="Displays info about text editor",
    # triggered=self.aboutHelp)
    triggered=self.newwindow)

    self.aboutQtAction = QAction("About &Qt", self,
    statusTip="Show the Qt library's About box",
    triggered=qApp.aboutQt)

    self.ss_image = QAction("Insert &.SWF(flash)", self,
    statusTip="Show the Qt library's About box",
    triggered=self.image_ss)

    self.actionAbout = QAction("image &Qt", self,
    statusTip="Show the Qt library's About box",
    triggered=self.openAbout)


# Actual menu bar item creation
 def CreateToolBar(self):
    """ Function to create actual menu bar
    """
    self.mainToolBar = self.addToolBar('Main')
    self.mainToolBar.addAction(self.newAction)
    self.mainToolBar.addSeparator()
    self.mainToolBar.addAction(self.copyAction)
    self.mainToolBar.addAction(self.pasteAction)

# Actual menu bar item creation
 def CreateMenus(self):
    """ Function to create actual menu bar
    """
    self.fileMenu = self.menuBar().addMenu("&File")
    self.fileMenu.addSeparator()
    self.editMenu = self.menuBar().addMenu("&Edit")
    self.helpMenu = self.menuBar().addMenu("&Help")
    self.formatMenu = self.menuBar().addMenu("F&ormat")
 def setAboutButton(self):
    """ Function to set About Button
    """
    self.aboutButton = QPushButton("About", self)
    self.aboutButton.move(110, 100)
    self.aboutButton.clicked.connect(self.showAbout)
 def showAbout(self):
        """ Function to show About Box
        """
        QMessageBox.about(self.aboutButton, "About PySide",
        "PySide is a cross-platform tool for generating GUI Programs.")

 def center(self):
    """ Function to center the application
    """
    qRect = self.frameGeometry()
    centerPoint = QDesktopWidget().availableGeometry().center()
    qRect.moveCenter(centerPoint)
    self.move(qRect.topLeft())
 def quitApp(self):
    """ Function to confirm a message from the user
    """
    userInfo = QMessageBox.question(self, 'Confirmation',
    "This will quit the application. Do you want to Continue?",
    QMessageBox.Yes | QMessageBox.No)
    if userInfo == QMessageBox.Yes:
        myApp.quit()
    if userInfo == QMessageBox.No:
       pass
 def setIconModes(self):
    myIcon1 = QIcon( self.icon)
    myLabel1 = QLabel('sample', self)
    pixmap1 = myIcon1.pixmap(50, 50, QIcon.Active, QIcon.On)
    myLabel1.setPixmap(pixmap1)
    myIcon2 = QIcon( self.icon)
    myLabel2 = QLabel('sample', self)
    pixmap2 = myIcon2.pixmap(50, 50, QIcon.Disabled, QIcon.Off)
    myLabel2.setPixmap(pixmap2)
    myLabel2.move(50, 0)
    myIcon3 = QIcon( self.icon)
    myLabel3 = QLabel('sample', self)
    pixmap3 = myIcon3.pixmap(50, 50, QIcon.Selected, QIcon.On)
    myLabel3.setPixmap(pixmap3)
    myLabel3.move(100, 0)
 def setIcon(self):
    """ Function to set Icon
    """
    from  PySide.QtGui import QPixmap
    pixmap = QPixmap(r'C:\Users\Hamed\Pictures\LED\led.jpg')
    # appIcon = QIcon('C:\Users\Hamed\Documents\soheil sites image\imageedit__9411602959.gif')
    appIcon = QIcon(pixmap)
    self.setWindowIcon(appIcon)

 def setButton(self):
    """ Function to add a quit button
    """
    myButton = QPushButton('Quit', self)
    myButton.move(20, 100)
    # myButton.clicked.connect(myApp.quit)
    myButton.clicked.connect(self.quitApp)
Beispiel #2
0
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initGUI()

    def initGUI(self):
        self.setWindowTitle("A Simple Text Editor")
        self.setWindowIcon(QIcon('appicon.png'))
        self.setGeometry(100, 100, 800, 600)
        self.center()

        # Text Editor
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.fileName = None

        self.filters = "Text files (*.txt)"
        # Setup and Show
        self.setupComponents()
        self.show()

    def center(self):
        """ Function to center the application """
        qRect = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())

    #=================================================================#
    # Create Central Widget Component to use QTextEdit
    #=================================================================#
    def setupComponents(self):
        """ Setting the central widget """

        # Progress Bar
        #self.progressBar = QProgressBar()
        #self.progressBar.setMinimum(0)
        #self.progressBar.setMaximum(100)

        # Status Label
        #self.statusLabel = QLabel('Status', self)

        # Status Bar
        self.myStatusBar = QStatusBar()
        #self.myStatusBar.addWidget(self.statusLabel, 1)
        #self.myStatusBar.addWidget(self.progressBar, 5)
        self.setStatusBar(self.myStatusBar)
        self.myStatusBar.showMessage('Ready', 1000)

        self.createActions()
        self.createMenus()
        self.createToolBar()

        # File Menu
        self.fileMenu.addAction(self.newAction)
        self.fileMenu.addAction(self.openAction)
        self.fileMenu.addAction(self.saveAction)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.exitAction)

        # Edit Menu
        self.editMenu.addAction(self.cutAction)
        self.editMenu.addAction(self.copyAction)
        self.editMenu.addAction(self.pasteAction)
        self.editMenu.addSeparator()
        self.editMenu.addAction(self.undoAction)
        self.editMenu.addAction(self.redoAction)
        self.editMenu.addSeparator()
        self.editMenu.addAction(self.selectAllAction)

        # Format Menu
        self.formatMenu.addAction(self.fontAction)

        # Help Menu
        self.helpMenu.addAction(self.aboutAction)
        self.helpMenu.addSeparator()
        self.helpMenu.addAction(self.aboutQtAction)

        # Tool Bar
        self.mainToolBar.addAction(self.newAction)
        self.mainToolBar.addAction(self.openAction)
        self.mainToolBar.addAction(self.saveAction)
        self.mainToolBar.addSeparator()
        self.mainToolBar.addAction(self.cutAction)
        self.mainToolBar.addAction(self.copyAction)
        self.mainToolBar.addAction(self.pasteAction)
        self.mainToolBar.addSeparator()
        self.mainToolBar.addAction(self.undoAction)
        self.mainToolBar.addAction(self.redoAction)

    # NOT-NEEDED : For progress bar
    def showProgress(self, progress):
        """ Function to show progress """
        self.progressBar.setValue(progress)
        if progress == 100:
            self.statusLabel.setText('Ready')
            return

    #=================================================================#
    # Helper Functions
    #=================================================================#
    def msgApp(self, title, message):
        """ Fuction to show dialog box with message """
        userInfo = QMessageBox.question(self, title, message,
                                        QMessageBox.Yes | QMessageBox.No)
        if userInfo == QMessageBox.Yes:
            return "Y"
        if userInfo == QMessageBox.No:
            return "N"
        self.close()

    #=================================================================#
    # Slots: called when the menu actions are triggered
    #=================================================================#
    def newFile(self):
        response = self.msgApp("Confirmation",
                               "Do you want to save the current file?")

        if response == "Y":
            self.saveFile()

        self.textEdit.setText('')
        self.fileName = None

    def openFile(self):
        response = self.msgApp("Confirmation",
                               "Save the current file before open a new one?")

        if response == "Y":
            self.saveFile()

        (self.fileName, self.filterName) = QFileDialog.getOpenFileName(self)
        self.textEdit.setText(open(self.fileName).read())

    def saveFile(self):
        if self.fileName == None or self.fileName == '':
            (self.fileName, self.filterName) = \
                           QFileDialog.getSaveFileName(self, filter=self.filters)

        if (self.fileName != ''):
            file = open(self.fileName, 'w')
            file.write(self.textEdit.toPlainText())
            self.statusBar().showMessage("File saved", 2000)

    def exitFile(self):
        response = self.msgApp("Confirmation",
                               "This will quit the application, continue?")
        if response == "Y":
            myApp.quit()
        else:
            pass

    def fontChange(self):
        (font, ok) = QFontDialog.getFont(QFont("Helvetica [Cronyx]", 10), self)
        if ok:
            self.textEdit.setCurrentFont(font)

    def aboutHelp(self):
        QMessageBox.about(self, "About Simple Text Editor",
                          "This example demonstrates the use of " + \
                          "a Menu Bar")

    def aboutQt(self):
        QMessageBox.aboutQt(self, title="Qt Version")

    #=================================================================#
    # Actions: send signals to slots
    #=================================================================#
    def createActions(self):
        """ Function to create actions for menus """
        # Parameters are as follows
        # 1. Icon to be displayed on the left
        # 2. The name, '&' means the the letter following
        #    will the be underlined (press 'Alt' to see)
        # 3. Implies the parent, which is the main window
        # 4. Shortcut key
        # 5. status tip in bottom status bar
        # 6. slot to be called
        self.newAction = QAction(QIcon('new.png'),
                                 '&New File',
                                 self,
                                 shortcut=QKeySequence.New,
                                 statusTip="Create a New File",
                                 triggered=self.newFile)

        self.openAction = QAction(QIcon('open.png'),
                                  '&Open File',
                                  self,
                                  shortcut=QKeySequence.Open,
                                  statusTip="Open an existing file",
                                  triggered=self.openFile)

        self.saveAction = QAction(QIcon('save.png'),
                                  '&Save File',
                                  self,
                                  shortcut=QKeySequence.Save,
                                  statusTip="Save the current file",
                                  triggered=self.saveFile)

        self.cutAction = QAction(
            QIcon('cut.png'),
            '&Cut',
            self,
            shortcut=QKeySequence.Cut,
            statusTip="Cut the current selection to clipboard",
            triggered=self.textEdit.cut)

        self.copyAction = QAction(QIcon('copy.png'),
                                  '&Copy',
                                  self,
                                  shortcut="Ctrl+C",
                                  statusTip="Copy",
                                  triggered=self.textEdit.copy)

        self.pasteAction = QAction(QIcon('paste.png'),
                                   '&Paste',
                                   self,
                                   shortcut="Ctrl+V",
                                   statusTip="Paste",
                                   triggered=self.textEdit.paste)

        self.selectAllAction = QAction(QIcon('selectAll.png'),
                                       '&Select All',
                                       self,
                                       statusTip="Select All",
                                       triggered=self.textEdit.selectAll)

        self.redoAction = QAction(QIcon('redo.png'),
                                  '&Redo',
                                  self,
                                  shortcut=QKeySequence.Redo,
                                  statusTip="Redo previous action",
                                  triggered=self.textEdit.redo)

        self.undoAction = QAction(QIcon('undo.png'),
                                  '&Undo',
                                  self,
                                  shortcut=QKeySequence.Undo,
                                  statusTip="Undo previous action",
                                  triggered=self.textEdit.undo)

        self.fontAction = QAction('F&ont',
                                  self,
                                  statusTip="Modify font properties",
                                  triggered=self.fontChange)

        self.aboutAction = QAction(QIcon('about.png'),
                                   '&About',
                                   self,
                                   statusTip="Displays info about text editor",
                                   triggered=self.aboutHelp)

        self.aboutQtAction = QAction(
            'About &Qt',
            self,
            statusTip="Show the Qt library's About box",
            triggered=self.aboutQt)

        self.exitAction = QAction(QIcon('exit.png'),
                                  '&Exit',
                                  self,
                                  shortcut="Ctrl+Q",
                                  statusTip="Exit the program",
                                  triggered=self.exitFile)

    #=================================================================#
    # Actual menu bar item creation
    #=================================================================#
    def createMenus(self):
        """ Function to create actual menu bar """
        self.fileMenu = self.menuBar().addMenu("&File")
        self.editMenu = self.menuBar().addMenu("&Edit")
        self.formatMenu = self.menuBar().addMenu("&Format")
        self.helpMenu = self.menuBar().addMenu("&Help")

    #=================================================================#
    # Tool Bar
    #=================================================================#
    def createToolBar(self):
        """ Function to create tool bar """
        self.mainToolBar = self.addToolBar('Main Tool Bar')