質問編集履歴
1
コード追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,175 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
![参考](0fb8df15b77d706fb45cf48f99a06a17.png)
|
28
|
+
|
29
|
+
#追記
|
30
|
+
|
31
|
+
組み上げ中のコードが入力数制限で記載できなかった為、
|
32
|
+
|
33
|
+
似たようなものを別途作成しました。
|
34
|
+
|
35
|
+
self.tableWidget.item(j, i).text() を空白のセル部分で取得しようとすると
|
36
|
+
|
37
|
+
アプリケーションが終了してしまいます。
|
38
|
+
|
39
|
+
引き続きご教示いただけますと幸いです。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
```python
|
44
|
+
|
45
|
+
import sys
|
46
|
+
|
47
|
+
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
|
48
|
+
|
49
|
+
from PyQt5.QtGui import QIcon
|
50
|
+
|
51
|
+
from PyQt5.QtCore import pyqtSlot
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
class App(QWidget):
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def __init__(self):
|
60
|
+
|
61
|
+
super().__init__()
|
62
|
+
|
63
|
+
self.title = "PyQt5 table - pythonspot.com"
|
64
|
+
|
65
|
+
self.left = 0
|
66
|
+
|
67
|
+
self.top = 0
|
68
|
+
|
69
|
+
self.width = 300
|
70
|
+
|
71
|
+
self.height = 200
|
72
|
+
|
73
|
+
self.initUI()
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
def initUI(self):
|
78
|
+
|
79
|
+
self.setWindowTitle(self.title)
|
80
|
+
|
81
|
+
self.setGeometry(self.left, self.top, self.width, self.height)
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
self.createTable()
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# Add box layout, add table to box layout and add box layout to widget
|
90
|
+
|
91
|
+
self.layout = QVBoxLayout()
|
92
|
+
|
93
|
+
self.layout.addWidget(self.tableWidget)
|
94
|
+
|
95
|
+
self.setLayout(self.layout)
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
# Show widget
|
100
|
+
|
101
|
+
self.show()
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
def createTable(self):
|
106
|
+
|
107
|
+
# Create table
|
108
|
+
|
109
|
+
self.tableWidget = QTableWidget()
|
110
|
+
|
111
|
+
self.tableWidget.setRowCount(4)
|
112
|
+
|
113
|
+
self.tableWidget.setColumnCount(4)
|
114
|
+
|
115
|
+
self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
|
116
|
+
|
117
|
+
self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
|
118
|
+
|
119
|
+
self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
|
120
|
+
|
121
|
+
#self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (2,2)"))
|
122
|
+
|
123
|
+
#self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
|
124
|
+
|
125
|
+
self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
|
126
|
+
|
127
|
+
self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
|
128
|
+
|
129
|
+
self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
|
130
|
+
|
131
|
+
self.tableWidget.move(0,0)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# table selection change
|
136
|
+
|
137
|
+
self.tableWidget.doubleClicked.connect(self.on_click)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
@pyqtSlot()
|
144
|
+
|
145
|
+
def on_click(self):
|
146
|
+
|
147
|
+
print("\n")
|
148
|
+
|
149
|
+
for currentQTableWidgetItem in self.tableWidget.selectedItems():
|
150
|
+
|
151
|
+
fragA = True
|
152
|
+
|
153
|
+
fragB = False
|
154
|
+
|
155
|
+
word =""
|
156
|
+
|
157
|
+
i = 0
|
158
|
+
|
159
|
+
j = 0
|
160
|
+
|
161
|
+
while fragA:
|
162
|
+
|
163
|
+
for s in range(4):
|
164
|
+
|
165
|
+
if self.tableWidget.item(j, i).text() != "":
|
166
|
+
|
167
|
+
fragB = True
|
168
|
+
|
169
|
+
else:break
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
i += 1
|
174
|
+
|
175
|
+
if fragB == True:
|
176
|
+
|
177
|
+
break
|
178
|
+
|
179
|
+
j=j+1
|
180
|
+
|
181
|
+
i = 0
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
#
|
186
|
+
|
187
|
+
print(self.tableWidget.item(1,1).text())
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
if __name__ == "__main__":
|
192
|
+
|
193
|
+
app = QApplication(sys.argv)
|
194
|
+
|
195
|
+
ex = App()
|
196
|
+
|
197
|
+
sys.exit(app.exec_())
|
198
|
+
|
199
|
+
```
|