質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

7990閲覧

Python GUIのTableでセルをクリックして内容を変更したい

gkan

総合スコア3

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/09/07 05:11

編集2020/09/07 05:14

PythonのGUI(PySimpleGuiなど)でTableにデータを表示させ、特定のセルを選択した時に〇×などの内容を反転させる方法を探しています。
調べた限りでは、実現する方法がわかりませんでした。(GitHubなども調べました)

PySimpleGuiに限らず、Tkinterや他のGUIライブラリ、Pythonであれば種類を問いません。
実現可能なのでしょうか?
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

PySimpleGUIは知りませんが、他のGUIツールキットでいいというのであれば、Qt5(PyQt5)とかGTK+(PyGObject+GTK3)とかでできると言えばできます。
画像を見る限りだとWindowsっぽいので、どちらかと言えばQt5の方がいいですかね。

PyQt5を使った例。(自分はQt5よりGTK+の方がよく使うので、こっちはもっとシンプルな書き方があるかもしれませんが)

python

1# coding: utf-8 2 3import sys 4from PyQt5.QtWidgets import * 5from PyQt5.QtCore import * 6from PyQt5.QtGui import * 7 8class ItemModel(QAbstractItemModel): 9 def __init__(self, parent=None): 10 super().__init__(parent) 11 self.items = [] 12 for i in range(10): 13 self.items += [(i, False)] 14 15 def columnCount(self, parent=QModelIndex()): 16 return 2 17 18 def data(self, index, role=Qt.DisplayRole): 19 if not index.isValid(): 20 return None 21 if index.row() >= len(self.items): 22 return None 23 if role != Qt.DisplayRole: 24 return None 25 row = index.row() 26 col = index.column() 27 if col == 0: 28 return str(self.items[row][0]) 29 elif col == 1: 30 return '○' if self.items[row][1] else '×' 31 return None 32 33 def index(self, row, column, parent=QModelIndex()): 34 if not parent.isValid() and row < len(self.items) and column < 2: 35 return self.createIndex(row, column) 36 return QModelIndex() 37 38 def parent(self, index): 39 return QModelIndex() 40 41 def rowCount(self, parent=QModelIndex()): 42 if parent.isValid(): 43 return 0 44 return len(self.items) 45 46 def toggleItem(self, index): 47 row = index.row() 48 self.items[row] = self.items[row][0], not self.items[row][1] 49 changedIndex = self.createIndex(row, 1) 50 self.dataChanged.emit(changedIndex, changedIndex, [Qt.DisplayRole]) 51 52class MainWindow(QWidget): 53 def __init__(self, parent=None): 54 super().__init__(parent) 55 56 layout = QVBoxLayout() 57 self.setLayout(layout) 58 59 self.listView = QTreeView() 60 self.listModel = ItemModel() 61 self.listView.setModel(self.listModel) 62 def onClicked(index): 63 if index.column() == 1: 64 self.listModel.toggleItem(index) 65 self.listView.clicked.connect(onClicked) 66 layout.addWidget(self.listView) 67 68if __name__ == '__main__': 69 app = QApplication(sys.argv) 70 71 window = MainWindow() 72 window.show() 73 74 sys.exit(app.exec_()) 75 76

PyGObject+GTK3を使った例。

python

1# coding: utf-8 2 3import sys 4import gi 5gi.require_version('Gtk', '3.0') 6from gi.repository import Gtk 7 8class CellRendererToggle(Gtk.CellRendererToggle): 9 __gtype_name__ = 'CellRendererToggle' 10 11 def do_render(self, cr, widget, background_area, cell_area, flags): 12 style_context = widget.get_style_context() 13 active = self.get_active() 14 layout = widget.create_pango_layout('○' if active else '×') 15 Gtk.render_layout(style_context, cr, cell_area.x, cell_area.y, layout) 16 17if __name__ == '__main__': 18 ui = ''' 19<interface> 20 <object class="GtkWindow" id="window"> 21 <signal name="destroy" handler="on_destroy" /> 22 <child> 23 <object class="GtkScrolledWindow" id="scrolled_window"> 24 <property name="visible">true</property> 25 <child> 26 <object class="GtkTreeView" id="tree_view"> 27 <property name="visible">true</property> 28 <property name="model">list_store</property> 29 <child> 30 <object class="GtkTreeViewColumn" id="column1"> 31 <child> 32 <object class="GtkCellRendererText" id="renderer1" /> 33 <attributes> 34 <attribute name="text">0</attribute> 35 </attributes> 36 </child> 37 </object> 38 </child> 39 <child> 40 <object class="GtkTreeViewColumn" id="column2"> 41 <child> 42 <object class="CellRendererToggle" id="renderer2"> 43 <signal name="toggled" handler="on_toggled" /> 44 </object> 45 <attributes> 46 <attribute name="active">1</attribute> 47 </attributes> 48 </child> 49 </object> 50 </child> 51 </object> 52 </child> 53 </object> 54 </child> 55 </object> 56 57 <object class="GtkListStore" id="list_store"> 58 <columns> 59 <column type="gchararray" /> 60 <column type="gboolean" /> 61 </columns> 62 </object> 63</interface> 64''' 65 builder = Gtk.Builder.new_from_string(ui, -1) 66 def on_toggled(renderer, path): 67 list_store[path][1] = not list_store[path][1] 68 builder.connect_signals({ 69 'on_destroy': Gtk.main_quit, 70 'on_toggled': on_toggled, 71 }) 72 73 list_store = builder.get_object('list_store') 74 for i in range(10): 75 list_store.append((str(i), False)) 76 77 window = builder.get_object('window') 78 window.show() 79 80 Gtk.main() 81 82

ただし、Qt5もGTK+も全然シンプルではありません
使いこなすには、それなりの時間がかかるか事を覚悟して下さい。
腰を据えて取り掛かるのであればいいですが、もしかするとPySimpleGUIでできる範囲の仕様に変えた方がいいかもしれません。

投稿2020/09/07 11:34

katsuko

総合スコア3491

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gkan

2020/09/08 05:27

サンプルまで提示していただき、ありがとうございました。 おっしゃる通りPySimpleGUIに比べるとシンプルではありませんが、機能・可読性考えて検討してみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問