我正在尝试在循环中创建一个简单的按钮表,但我的代码不起作用。没有错误,但我没有看到执行结果。
import sys
from PyQt4 import QtGui, QtCore
names = ['X+', 'X-', 'Y+',\
'Y-', 'Z+', 'Z-',\
'A1+', 'A1-', 'A2+',\
'A2-', 'A3+', 'A3-',\
'Hand']
pos = [(50, 100), (200, 100), (50, 200),\
(200,200), (50, 300), (200,300),\
(370, 100), (520, 100), (370, 200),\
(520, 200), (370, 300), (520, 300),\
(50, 400)]
size = [(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(226, 74)]
class Window(QtGui.QMainWindow):
...........
def createButtons(self):
index = 0
self.buttons = []
for i in names:
self.buttons.append(index)
self.buttons[index] = QtGui.QPushButton(self)
self.buttons[index].setText(i)
self.buttons[index].setGeometry(pos[index][0], pos[index][1], size[index][0], size[index][1])
index += 1
def runApp():
app = QtGui.QApplication(sys.argv)
gui = Window()
gui.createButtons()
gui.show()
sys.exit(app.exec_())
runApp()
你能帮帮我吗?
名字 - 名单,
pos - 位置列表[x,y],
size - w和h的列表。
答案 0 :(得分:1)
您只需更改尺寸即可看到按钮。您必须使用resize()
import sys
from PyQt4 import QtGui, QtCore
names = ['X+', 'X-', 'Y+',\
'Y-', 'Z+', 'Z-',\
'A1+', 'A1-', 'A2+',\
'A2-', 'A3+', 'A3-',\
'Hand']
pos = [(50, 100), (200, 100), (50, 200),\
(200,200), (50, 300), (200,300),\
(370, 100), (520, 100), (370, 200),\
(520, 200), (370, 300), (520, 300),\
(50, 400)]
size = [(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(74, 74), (74, 74), (74, 74),\
(226, 74)]
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
def createButtons(self):
index = 0
self.buttons = []
for i in names:
self.buttons.append(index)
self.buttons[index] = QtGui.QPushButton(self)
self.buttons[index].setText(i)
self.buttons[index].setGeometry(pos[index][0], pos[index][1], size[index][0], size[index][1])
index += 1
self.resize(650, 500)
def runApp():
app = QtGui.QApplication(sys.argv)
gui = Window()
gui.createButtons()
gui.show()
sys.exit(app.exec_())
runApp()
截图: