Skip to content

Instantly share code, notes, and snippets.

@mieki256
Last active November 17, 2016 22:33
Show Gist options
  • Save mieki256/8fe536a8d39cd9481979470f807eb3de to your computer and use it in GitHub Desktop.
Save mieki256/8fe536a8d39cd9481979470f807eb3de to your computer and use it in GitHub Desktop.
PySideを使ってツールバーの表示をテスト
#!python
# -*- mode: python; Encoding: utf-8; coding: utf-8 -*-
# Last updated: <2016/11/18 07:05:41 +0900>
u"""
PySide, QToolBar test.
ツールバーを作ってみるテスト。
Windows10 x64 + Python 2.7.12 + PySide 1.2.4 で動作確認。
"""
import sys
from PySide.QtCore import * # NOQA
from PySide.QtGui import * # NOQA
# from PySide import QtSvg, QtXml # NOQA
class MyMainWindow(QMainWindow):
u"""メインウインドウ."""
def __init__(self, parent=None):
"""init."""
super(MyMainWindow, self).__init__(parent)
# for s in QImageReader.supportedImageFormats():
# print(s)
self.load_toolbar_image()
self.init_menubar()
self.init_statusbar()
self.init_cwidget()
def load_toolbar_image(self):
u"""ツールバー用の画像読み込み."""
im = QImage("./tmp_toolbar_img.png")
# アイコンサイズで画像を分割
w, h = 32, 32
self.icons = []
for x in range(im.width() / w):
nim = im.copy(x * w, 0, w, h)
self.icons.append(QPixmap(nim))
def init_menubar(self):
u"""メニューバーの初期化."""
new_action = QAction("&New", self)
new_action.setToolTip("New Canvas")
new_icon = QIcon(self.icons[9])
# new_icon = self.style().standardIcon(QStyle.SP_FileIcon)
new_action.setIcon(new_icon)
new_action.setShortcut(QKeySequence(QKeySequence.New))
new_action.triggered.connect(self.new_canvas)
open_action = QAction("&Open", self)
open_action.setToolTip("Open Image")
open_icon = QIcon(self.icons[10])
# open_icon = self.style().standardIcon(QStyle.SP_DialogOpenButton)
open_action.setIcon(open_icon)
open_action.setShortcut(QKeySequence(QKeySequence.Open))
open_action.triggered.connect(self.open_image)
save_action = QAction("&Save", self)
save_action.setToolTip("Save Canvas")
save_icon = QIcon(self.icons[11])
# save_icon = self.style().standardIcon(QStyle.SP_DialogSaveButton)
save_action.setIcon(save_icon)
save_action.setShortcut(QKeySequence(QKeySequence.Save))
save_action.triggered.connect(self.save_canvas)
exit_action = QAction("&Exit", self)
exit_action.setToolTip("Exit application")
exit_action.setIcon(QIcon(self.icons[18]))
exit_action.setShortcut('Alt+X')
exit_action.triggered.connect(qApp.quit)
undo_action = QAction("&Undo", self)
undo_action.setIcon(QIcon(self.icons[12]))
undo_action.setShortcut(QKeySequence(QKeySequence.Undo))
undo_action.triggered.connect(self.undo)
zoomin_action = QAction("Zoom &In", self)
zoomin_action.setIcon(QIcon(self.icons[14]))
zoomin_action.setShortcut(QKeySequence(QKeySequence.ZoomIn))
zoomin_action.triggered.connect(self.zoom_in)
zoomout_action = QAction("Zoom &Out", self)
zoomout_action.setIcon(QIcon(self.icons[15]))
zoomout_action.setShortcut(QKeySequence(QKeySequence.ZoomOut))
zoomout_action.triggered.connect(self.zoom_out)
zoomact_action = QAction("Zoom &1:1", self)
zoomact_action.setIcon(QIcon(self.icons[16]))
zoomact_action.setShortcut("Ctrl+1")
zoomact_action.triggered.connect(self.zoom_actual)
zoomfit_action = QAction("Zoom &Fit", self)
zoomfit_action.setIcon(QIcon(self.icons[17]))
# zoomfit_action.setShortcut("Ctrl+0")
zoomfit_action.triggered.connect(self.zoom_fit)
# ツール群。checkableをTrueにしてることに注意
pen_action = QAction("Pen", self)
pen_action.setIcon(QIcon(self.icons[0]))
pen_action.setCheckable(True)
pen_action.setShortcut("P")
pen_action.triggered.connect(self.pen_tool)
eraser_action = QAction("Eraser", self)
eraser_action.setIcon(QIcon(self.icons[2]))
eraser_action.setCheckable(True)
eraser_action.setShortcut("E")
eraser_action.triggered.connect(self.eraser_tool)
# こういう書き方もできる
line_action = QAction(QIcon(self.icons[3]), "Line", self,
checkable=True, shortcut="L",
statusTip="Line",
triggered=self.line_tool)
rect_action = QAction(QIcon(self.icons[4]), "Rectangle", self,
checkable=True, shortcut="R",
triggered=self.rectangle_tool)
rect_fill_action = QAction(QIcon(self.icons[5]), "Rectangle fill",
self, checkable=True, shortcut="A",
triggered=self.rectangle_fill_tool)
fill_action = QAction(QIcon(self.icons[6]), "Fill", self,
checkable=True, shortcut="F",
triggered=self.fill_tool)
picker_action = QAction(QIcon(self.icons[7]), "Color Picker", self,
checkable=True, shortcut="C",
triggered=self.color_picker_tool)
sel_action = QAction(QIcon(self.icons[8]), "Select", self,
checkable=True, shortcut="S",
triggered=self.select_tool)
# ツール群はグループ化する。どれか一つだけが選ばれる状態になる。
self.tools_grp = QActionGroup(self)
self.tools_grp.addAction(pen_action)
self.tools_grp.addAction(eraser_action)
self.tools_grp.addAction(line_action)
self.tools_grp.addAction(rect_action)
self.tools_grp.addAction(rect_fill_action)
self.tools_grp.addAction(fill_action)
self.tools_grp.addAction(picker_action)
self.tools_grp.addAction(sel_action)
pen_action.setChecked(True)
# メニューバーに登録
mb = self.menuBar()
file_menu = mb.addMenu("&File")
file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addSeparator()
file_menu.addAction(exit_action)
edit_menu = mb.addMenu("&Edit")
edit_menu.addAction(undo_action)
view_menu = mb.addMenu("&View")
view_menu.addAction(zoomin_action)
view_menu.addAction(zoomout_action)
view_menu.addAction(zoomact_action)
view_menu.addAction(zoomfit_action)
tools_menu = mb.addMenu("&Tools")
tools_menu.addAction(pen_action)
tools_menu.addAction(eraser_action)
tools_menu.addAction(line_action)
tools_menu.addAction(rect_action)
tools_menu.addAction(rect_fill_action)
tools_menu.addAction(fill_action)
tools_menu.addAction(picker_action)
tools_menu.addAction(sel_action)
# ファイル関係ツールバーを設定
self.file_tb = QToolBar("File")
self.addToolBar(self.file_tb)
self.file_tb.addAction(exit_action)
self.file_tb.addSeparator()
self.file_tb.addAction(new_action)
self.file_tb.addAction(open_action)
self.file_tb.addAction(save_action)
self.file_tb.addSeparator()
self.file_tb.addAction(undo_action)
self.file_tb.setIconSize(QSize(32, 32))
# ツール関係ツールバーを設定
self.tools_tb = QToolBar("Tools")
self.addToolBar(self.tools_tb)
self.tools_tb.addAction(pen_action)
self.tools_tb.addAction(eraser_action)
self.tools_tb.addAction(line_action)
self.tools_tb.addAction(rect_action)
self.tools_tb.addAction(rect_fill_action)
self.tools_tb.addAction(fill_action)
self.tools_tb.addAction(picker_action)
self.tools_tb.addAction(sel_action)
self.tools_tb.setIconSize(QSize(32, 32))
# ズーム関係ツールバーを設定
self.view_tb = QToolBar("View")
self.addToolBar(self.view_tb)
self.view_tb.addAction(zoomin_action)
self.view_tb.addAction(zoomout_action)
self.view_tb.addAction(zoomact_action)
self.view_tb.addAction(zoomfit_action)
self.view_tb.setIconSize(QSize(32, 32))
def init_statusbar(self):
"""init statusbar."""
self.statusBar()
self.statusBar().showMessage("Ready.")
def init_cwidget(self):
"""init central widget."""
scene = QGraphicsScene()
pm = QPixmap("./tmp_bg.png")
scene.addPixmap(pm)
self.gview = QGraphicsView(self)
self.gview.setScene(scene)
self.setCentralWidget(self.gview)
def new_canvas(self):
"""new canvas."""
self.statusBar().showMessage("New Canvas.")
def open_image(self):
"""open image."""
self.statusBar().showMessage("Open Image.")
def save_canvas(self):
"""save canvas."""
self.statusBar().showMessage("Save Canvas.")
def undo(self):
"""undo."""
self.statusBar().showMessage("Undo.")
def zoom_in(self):
"""zoom in."""
self.statusBar().showMessage("Zoom In.")
def zoom_out(self):
"""zoom out."""
self.statusBar().showMessage("Zoom Out.")
def zoom_actual(self):
"""zoom actual."""
self.statusBar().showMessage("Zoom Actual.")
def zoom_fit(self):
"""zoom fit."""
self.statusBar().showMessage("Zoom Fit.")
def pen_tool(self):
"""pen tool."""
self.statusBar().showMessage("Select Pen tool")
def eraser_tool(self):
"""eraser tool."""
self.statusBar().showMessage("Select Eraser tool")
def line_tool(self):
"""line tool."""
self.statusBar().showMessage("Select Line tool")
def rectangle_tool(self):
"""rectangle tool."""
self.statusBar().showMessage("Select Rectangle tool")
def rectangle_fill_tool(self):
"""rectangle fill tool."""
self.statusBar().showMessage("Select Rectangle fill tool")
def fill_tool(self):
"""fill tool."""
self.statusBar().showMessage("Select Fill tool")
def color_picker_tool(self):
"""color picker tool."""
self.statusBar().showMessage("Select Color picker tool")
def select_tool(self):
"""select tool."""
self.statusBar().showMessage("Select Select tool")
def main():
"""main."""
app = QApplication(sys.argv)
# qApp.setStyle(QStyleFactory.create('Cleanlooks'))
w = MyMainWindow()
w.setWindowTitle("PySide QToolBar Test")
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment