简体   繁体   English

如何使用PyQt4在Python中并排放置三个图标?

[英]How to put three icons side by side in Python using PyQt4?

I wrote a small program in Python and for now I have 3 buttons side by side and I am able to put an icon on each of them. 我用Python编写了一个小程序,现在我有3个并排的按钮,并且每个按钮上都可以放置一个图标。 How to display only icons instead of buttons with icons? 如何仅显示图标而不显示带有图标的按钮? I hope you know what I mean. 我希望你明白我的意思。 I want to remove my buttons and display icons only. 我只想删除按钮并只显示图标。

Here is my code: 这是我的代码:

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):
 def __init__(self):
  super(Example, self).__init__()
  self.initUI()

 def initUI(self):
  self.statusBar().showMessage('Start the game!')
  self.setGeometry(300, 300, 250, 150)
  panel = QtGui.QWidget()
  grid = QtGui.QGridLayout()
  p1 = QtGui.QPushButton("")
  p1.setIcon(QtGui.QIcon('image1.jpg'))
  p2 = QtGui.QPushButton("")
  p2.setIcon(QtGui.QIcon('image2.jpg'))
  p3 = QtGui.QPushButton("")
  p3.setIcon(QtGui.QIcon('image3.jpg'))

  p1.setMaximumHeight(1000)
  p2.setMaximumHeight(1000)
  p3.setMaximumHeight(1000)

  grid.addWidget(p1,0,0)
  grid.addWidget(p2,0,1)
  grid.addWidget(p3,0,2)

  panel.setLayout(grid)
  self.setCentralWidget(panel)
  self.setWindowTitle('MyGame')    
  self.show()

def main():
  app = QtGui.QApplication(sys.argv)
  ex = Example()
  sys.exit(app.exec_())

if __name__ == '__main__':
 main()

use the .setGeometry method on your QLabel ( lbl ) 在QLabel( lbl )上使用.setGeometry方法

If you want the image to be on the far right, you could add a space item to your HBoxLayout like this: 如果希望图像位于最右边,则可以像这样在HBoxLayout中添加一个空格项:

lbl.setGeometry(0,0,100,100)
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
hbox.addItem(spacerItem)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM