2

这是我第一个使用 PyQt4 的应用程序。

我有以下两个问题

在此处输入图像描述

  1. 左侧框中的按钮。它们的间距不同。

  2. 单选按钮是否可以使用斜体?(即,Ke等人, 而不是 Ke 等人)?

从 PyQt4 导入 sys 导入 QtGui,QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        # title of the window
        self.setWindowTitle('Segmentation Accuracy')
        # fix window size
        self.setFixedSize(400,200)
        # status bar with initial message
        self.statusBar().showMessage('ready')


        # Layout
        # contiene gli altri widget
        cWidget = QtGui.QWidget(self)
        grid = QtGui.QGridLayout(cWidget) 


        # Box Left
        vBox_left = QtGui.QVBoxLayout()
        vBox_left.setSpacing(2) #set spacing

        Button_input1 = QtGui.QPushButton("Reference Polygons")
        vBox_left.addWidget(Button_input1)

        Button_input2 = QtGui.QPushButton("Segmented Polygons")
        vBox_left.addWidget(Button_input2)

        Button_output = QtGui.QPushButton("Save")
        vBox_left.addWidget(Button_output)

        Button_start = QtGui.QPushButton("Start")
        Button_start.setCheckable(True)

        vBox_left.addWidget(Button_input1)
        vBox_left.addWidget(Button_input2)
        vBox_left.addWidget(Button_output)
        vBox_left.addWidget(Button_start)

        # Box right
        vBox_rigth = QtGui.QVBoxLayout()
        vBox_rigth.setSpacing(10) #set spacing
        ke = QtGui.QRadioButton("Ke et al.", cWidget)
        pu = QtGui.QRadioButton("Pu et al.", cWidget)
        clinton = QtGui.QRadioButton("Clinton et al.", cWidget)
        ke.setChecked(True)
        vBox_rigth.addWidget(ke)
        vBox_rigth.addWidget(pu)
        vBox_rigth.addWidget(clinton)

        ke.setStatusTip("Ke et al., 2010. Remote Sensing of Environment, 114, pp. 1141-1154")
        pu.setStatusTip("Pu et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")
        clinton.setStatusTip("Clinton et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")

        grid.addWidget(Button_input1, 0, 0)
        grid.addWidget(Button_input2, 1, 0)
        grid.addWidget(Button_output, 2, 0)
        grid.addWidget(Button_start, 3, 0)
        grid.addLayout(vBox_left, 0, 0)
        grid.addLayout(vBox_rigth, 0, 1)


        cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)

def main():
    app = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

main()
4

1 回答 1

2

您使用QGridLayout并放入Button_input1单元格 (0,0) 和vBox_rigth包含三个raido 按钮的布局放入单元格 (0,1)。它们被调整到相同的高度。这就是为什么你的四个按钮没有相同的间距。

    grid = QtGui.QGridLayout(cWidget)
    ......
    grid.addWidget(Button_input1, 0, 0)
    ......
    grid.addLayout(vBox_rigth, 0, 1)

您不应该使用addWidget将这些按钮添加到vBox_left两次。


    Button_input1 = QtGui.QPushButton("Reference Polygons")
    vBox_left.addWidget(Button_input1)

    Button_input2 = QtGui.QPushButton("Segmented Polygons")
    vBox_left.addWidget(Button_input2)

    Button_output = QtGui.QPushButton("Save")
    vBox_left.addWidget(Button_output)

    Button_start = QtGui.QPushButton("Start")
    Button_start.setCheckable(True)

    vBox_left.addWidget(Button_input1)
    vBox_left.addWidget(Button_input2)
    vBox_left.addWidget(Button_output)
    vBox_left.addWidget(Button_start)

而且,这四个按钮已经被添加到 QVBoxLayoutvBox_left中,所以你不应该将它们添加到grid.

    grid.addWidget(Button_input1, 0, 0)
    grid.addWidget(Button_input2, 1, 0)
    grid.addWidget(Button_output, 2, 0)
    grid.addWidget(Button_start, 3, 0)

您可以为小部件使用斜体。您只需要更改font属性。请参阅http://doc.qt.digia.com/stable/qwidget.html#font-prop
例如:

    ke = QtGui.QRadioButton("Ke et al.", cWidget)
    font = QtGui.QFont("Times", 10)
    font.setItalic(True)
    ke.setFont(font)

您还可以QLabel用来显示富文本。例如:

    ke_label = QtGui.QLabel("Ke <i>et al</i>.")

因此,决赛__init__可能是

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        # title of the window
        self.setWindowTitle('Segmentation Accuracy')
        # fix window size
        self.setFixedSize(400,200)
        # status bar with initial message
        self.statusBar().showMessage('ready')


        # Layout
        # contiene gli altri widget
        cWidget = QtGui.QWidget(self)
        grid = QtGui.QGridLayout(cWidget)

        # Box Left
        vBox_left = QtGui.QVBoxLayout()
        vBox_left.setSpacing(2) #set spacing

        Button_input1 = QtGui.QPushButton("Reference Polygons")
        vBox_left.addWidget(Button_input1)

        Button_input2 = QtGui.QPushButton("Segmented Polygons")
        vBox_left.addWidget(Button_input2)

        Button_output = QtGui.QPushButton("Save")
        vBox_left.addWidget(Button_output)

        Button_start = QtGui.QPushButton("Start")
        Button_start.setCheckable(True)

        # vBox_left.addWidget(Button_input1)
        # vBox_left.addWidget(Button_input2)
        # vBox_left.addWidget(Button_output)
        vBox_left.addWidget(Button_start)

        # Box right
        vBox_right = QtGui.QVBoxLayout()
        vBox_right.setSpacing(10) #set spacing

        ke = QtGui.QRadioButton("", cWidget)
        pu = QtGui.QRadioButton("", cWidget)
        clinton = QtGui.QRadioButton("", cWidget)
        ke.setChecked(True)

        ke_label = QtGui.QLabel("Ke <i>et al</i>.", cWidget)
        pu_label = QtGui.QLabel("Pu et al.", cWidget)
        clinton_label = QtGui.QLabel("Clinton et al.", cWidget)

        grid_right = QtGui.QGridLayout()

        grid_right.addWidget(ke, 0, 0)
        grid_right.addWidget(pu, 1, 0)
        grid_right.addWidget(clinton, 2, 0)
        grid_right.addWidget(ke_label, 0, 1)
        grid_right.addWidget(pu_label, 1, 1)
        grid_right.addWidget(clinton_label, 2, 1)
        grid.addLayout(grid_right, 0, 1)

        grid.setColumnStretch(0, 1)
        grid.setColumnStretch(1, 1)
        grid_right.setColumnStretch(1, 1)

        ke.setStatusTip("Ke et al., 2010. Remote Sensing of Environment, 114, pp. 1141-1154")
        pu.setStatusTip("Pu et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")
        clinton.setStatusTip("Clinton et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")


        ke_label.setStatusTip("Ke et al., 2010. Remote Sensing of Environment, 114, pp. 1141-1154")
        pu_label.setStatusTip("Pu et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")
        clinton_label.setStatusTip("Clinton et al., 2012. Remote Sensing of Environment, 124, pp. 516-533")

        # grid.addWidget(Button_input1, 0, 0)
        # grid.addWidget(Button_input2, 1, 0)
        # grid.addWidget(Button_output, 2, 0)
        # grid.addWidget(Button_start, 3, 0)
        grid.addLayout(vBox_left, 0, 0)
        #grid.addLayout(vBox_right, 0, 1)


        # cWidget.setLayout(grid)
        self.setCentralWidget(cWidget)
于 2013-03-12T17:53:04.737 回答