いわて駐在研究日誌2。

NEVER STAND BEHIND ME

PyQtでGUIアプリ (2)

TabWidgetでの作成例は、ネットに転がってたので下記のとおり。

#  Tab1.py (coding UTF8)
#  refs.:  http://zetcode.com/tutorials/pyqt4/dialogs/
#       http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtabbar.html
#
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui

class Ui_TabWidget(object):

    def setupUi(self, TabWidget):
        TabWidget.setObjectName("TabWidget")
        TabWidget.resize(640, 480)
 
        self.tabBar=QtGui.QTabBar(TabWidget)

        self.tabBar.addTab("Tab1")
        self.tabBar.setTabText(0,"初期条件")
        self.tabBar.addTab("Tab2")
        self.tabBar.setTabText(1,"境界条件")
        self.tabBar.addTab("Tab3")
        self.tabBar.setTabText(2,"日射条件")
 
        TabWidget.setTabBar(self.tabBar)
        self.retranslateUi(TabWidget)
 
        QtCore.QMetaObject.connectSlotsByName(TabWidget)
 
    def retranslateUi(self, TabWidget):

        TabWidget.setWindowTitle(QtGui.QApplication.translate("TabWidget","TabWidget", None, QtGui.QApplication.UnicodeUTF8))
 
 
app = QtGui.QApplication(sys.argv)	#Qtアプリケーションオブジェクトを作る
                			#このオブジェクトハンドルはマウスや
                        		#キーボードのイベントを取得する大本になる

TabWidget = QtGui.QTabWidget()
ui = Ui_TabWidget()
ui.setupUi(TabWidget)

TabWidget.show()
sys.exit(app.exec_())

QMainWindosをつかうとこんな感じ?

#  Tab2.py (coding UTF8)
#  refs.:  http://d.hatena.ne.jp/t2y-1979/20100917/1284652234
#       http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtabbar.html
#
# -*- coding: utf-8 -*-
import sys, os, codecs
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle("Main Window")
        self.resize(800, 600)
        self.setMinimumSize(800, 600)


        # Setup Actions
        ## openFile Action
        openFile = QAction(QIcon('open.png'), 'Open', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open File')
        self.connect(openFile, SIGNAL('triggered()'), self.showOpenDialog)
        ## saveFile Action
        saveFile = QAction(QIcon('save.png'), 'Save', self)
        saveFile.setShortcut('Ctrl+S')
        saveFile.setStatusTip('Save File')
        self.connect(saveFile, SIGNAL('triggered()'), self.showSaveDialog)
        ## Quit Action
        Quit = QAction(QIcon('quit.png'), 'Quit', self)
        Quit.setShortcut('Ctrl+Q')
        Quit.setStatusTip('Quit this program')
        self.connect(Quit, SIGNAL('triggered()'), self.QuitDialog)

        # 1.set Menu Bar
        self.file_menu = self.menuBar().addMenu("&File")
        ## Add  Actions to Menu Bar
        self.file_menu.addAction(openFile)
        self.file_menu.addAction(saveFile)
        self.file_menu.addAction(Quit)
       
        # 2.set Tool Bar
        self.file_tool = self.addToolBar("ToolBar")
        self.file_tool.addAction(openFile)
        self.file_tool.addAction(saveFile)
        self.file_tool.addAction(Quit)

        # 3.set Status Bar
        self.status_bar = QStatusBar(self)
        self.status_bar.showMessage("Ready", 5000)
        self.setStatusBar(self.status_bar)

        # Tab Widget Setup
        self.tab_widget = QTabWidget(self)
        self.tab1 = QTextEdit() ###
        self.tab2 = QTextEdit() ###
        self.tab3 = QTextEdit() ###
        self.setCentralWidget(self.tab_widget) #QMainWindowのセントラルウィジェットをQTabWidgetとする
        self.tab_widget.setTabPosition(QTabWidget.South)
        self.tab_widget.addTab(self.tab1, "Tab 1")
        self.tab_widget.addTab(self.tab2, "Tab 2")
        self.tab_widget.addTab(self.tab3, "Tab 3")
    
    def showOpenDialog(self):

        filename = QFileDialog.getOpenFileName(self, 'Open file', '')
        from os.path import isfile
        if isfile(filename): 
            fname = codecs.open(filename,'r','utf-8') #UTF-8のファイルを読み込み     
            data = fname.read()
            
            self.tab1.setText(data) ###
            
            self.setWindowTitle("Main Window: %s" % os.path.abspath(filename))
            
    def showSaveDialog(self):

        savename = QFileDialog.getSaveFileName(self, 'Save file', '')
        fname = codecs.open(savename, 'w', 'utf-8') #UTF-8のファイルに書き込み
        
        fname.write(self.tab1.toPlainText()) ###

    def QuitDialog(self):
        
        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes |
                  QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            quit()
        else:
            return  
 
# QMainWindowを使った表示

def main():

    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()