簡體   English   中英

使用pyqt4和python導入簡單程序

[英]importing simple program using pyqt4 and python

from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
        QLabel, QVBoxLayout, QWidget
from PyQt4 import QtGui
import sys

import subprocess

class MainWindow1(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent) 
        button = QPushButton('NotePad')

        label = QLabel('MainWindow1')

        centralWidget = QWidget()
        vbox = QVBoxLayout(centralWidget)
        vbox.addWidget(label)
        vbox.addWidget(button)
        self.setCentralWidget(centralWidget)

        button.clicked.connect(self.LaunchNotepad)

    # Some code here - including import subprocess
    def LaunchNotepad(self):

        returncode = subprocess.call(['python', 'notepad.py'])




if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwindow1 = MainWindow1()
    mainwindow1.show()
    sys.exit(app.exec_())

該代碼創建了一個帶有按鈕的主窗口,當我按下按鈕時,我希望它導入名為“ notepad”的文件,(我認為是),但是它會立即打開並關閉它。 在關閉它之前,我需要能夠使用該程序的記事本,在這種情況下,它應該恢復為原始窗口。 最終,我將有3個或4個按鈕導入3個或4個不同的程序

我不認為記事本中有錯誤,因為當我只有“ import notepad”語句時,它可以完美運行

注意:記事本文件只是一個簡單的文本程序(非常類似於Windows pc上的“記事本”程序)

提前致謝

在這里編輯是記事本代碼:

import sys
import os
import datetime as dt
from PyQt4 import QtGui
from PyQt4 import *


class Notepad(QtGui.QMainWindow):
    def __init__(self):
        super(Notepad, self).__init__()
        self.initUI()






    def initUI(self):
        newAction = QtGui.QAction('New', self)
        newAction.setShortcut('Ctrl+N')
        newAction.setStatusTip('Create new file')
        newAction.triggered.connect(self.newFile)  
        saveAction = QtGui.QAction('Save', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.setStatusTip('Save current file')
        saveAction.triggered.connect(self.saveFile)
        openAction = QtGui.QAction('Open', self)
        openAction.setShortcut('Ctrl+O')
        openAction.setStatusTip('Open a file')
        openAction.triggered.connect(self.openFile)

        closeAction = QtGui.QAction('Close', self)
        closeAction.setShortcut('Ctrl+Q')
        closeAction.setStatusTip('Close Notepad')
        closeAction.triggered.connect(self.close)
        menubar = self.menuBar()

        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(newAction)
        fileMenu.addAction(saveAction)
        fileMenu.addAction(openAction)
        fileMenu.addAction(closeAction)

        #help menu
        helpMenu = menubar.addMenu('&Help')
        aboutAction = QtGui.QAction('About', self)
        aboutAction.setShortcut('Ctrl+A')
        aboutAction.setStatusTip('About')
        helpMenu.addAction(aboutAction)
        aboutAction.triggered.connect(self.about) 

        self.text = QtGui.QTextEdit(self)

        self.setCentralWidget(self.text)
        self.setGeometry(300,300,300,300)
        self.setWindowTitle('Notepad')


        self.show()
        self.statusBar()










    def newFile(self):
        self.text.clear()

    def saveFile(self):
        filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', os.getenv('HOME'))
        f = open(filename, 'w')
        filedata = self.text.toPlainText()
        f.write(filedata)
        f.close()

    def openFile(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
        f = open(filename, 'r')
        filedata = f.read()
        self.text.setText(filedata)
        f.close()

        self.setGeometry(300,300,300,300)
        self.setWindowTitle('Notepad')
    self.show()
    def closeEvent(self, event):


        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
    def about(self, event):
        reply = QtGui.QMessageBox.question(self, 'About Task Manager',
            "This is a notepad todo list program written by craig murch")


        return Notepad



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    notepad = Notepad()

    sys.exit(app.exec_())

編輯:上面編輯的代碼現在確實希望我想要它做,但是它也加載了我不想的cmd,如何停止加載cmd?

好吧,在記事本代碼中

sys.exit(app.exec_())

這將關閉整個過程 因此,如果要從父窗口導入它,是的,它應該正在關閉您的應用程序。

另外,無論使用哪種GUI框架,在同一過程中混合主循環總是一個壞主意。 相反,您應該使用subprocess來調用其他應用程序:

# Some code here - including import subprocess
import os
def LaunchNotepad(self):
    self.DoSomething() #Or whatever you want to do before your program launches
    returncode = subprocess.call(['pythonw', 'notepad.py'],
                                 stdout=open(os.devnull, 'w'),
                                 stderr=open(os.devnull, 'w'))
    self.ShowMe() #Won't run until notepad finishes
    if not returncode:
        self.ShowError("Notepad exited abnormally!")

那是您可以做什么的一個非常基本的例子。

乍一看,我不能說在類定義中使用它是行不通的:

app = QtGui.QApplication(sys.argv)
notepad = Notepad()

sys.exit(app.exec_())

那是你要做的事

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    notepad = Notepad()
    notepad.show()  # call show here
    sys.exit(app.exec_())

不作為類定義的一部分。

並且self.show() initUI是最佳選擇,默認情況下,gui對象也不應該顯示自身,如果要實例化一個組件然后將其添加到另一個組件中,這是沒有意義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM