在Python中将QScrollBar添加到狭窄的QWidget中

时间:2017-05-17 18:16:14

标签: python pyqt pyqt4 pyside qscrollarea

更新上一个问题。

当显示这看起来非常狭窄时,请参见下面的照片。

enter image description here

我想保持QWidget(面板)的高度相同,并在其右侧添加一个QScrollBar,这样小部件就会自然缩小,用户可以向下滚动

我尝试添加此代码。

scroll = QtGui.QScrollArea()
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout.addWidget(scroll)

但是代码运行,但是没有GUI打开,所以我手动强制从命令行停止它。

工作代码如下所示。

谢谢

罗布

import sys
from PyQt4 import QtGui
from PySide import QtCore, QtGui
from scapy.all import *
from PySide.QtCore import *
from PySide.QtGui import *

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
layout = QtGui.QGridLayout()
vbox1 = QtGui.QVBoxLayout()

button = QPushButton("Send")

flag1 = QCheckBox("CWR")
flag2 = QCheckBox("ECN")
flag3 = QCheckBox("URG")
flag4 = QCheckBox("ACK")
flag5 = QCheckBox("PSH")
flag6 = QCheckBox("RST")
flag7 = QCheckBox("SYN")
flag8 = QCheckBox("FIN")

textEdit1 = QtGui.QTextEdit('')
textEdit2 = QtGui.QTextEdit('')
textEdit3 = QtGui.QTextEdit('')
textEdit4 = QtGui.QTextEdit('')
#textEdit5 = QtGui.QTextEdit('')#using check boxes instead
textEdit6 = QtGui.QTextEdit('')

def buildTCPPanal():
    label1 = QtGui.QLabel("IP Destanation Address")
    label2 = QtGui.QLabel("IP Source Address")
    label3 = QtGui.QLabel("Source Port")
    label4 = QtGui.QLabel("Destanation Port")
    label5 = QtGui.QLabel("Flags")
    label6 = QtGui.QLabel("Data")

    layout.addWidget(label1) #dest add
    layout.addWidget(textEdit1)

    layout.addWidget(label2) #src add
    layout.addWidget(textEdit2)

    layout.addWidget(label3) #src port
    layout.addWidget(textEdit3)

    layout.addWidget(label4) #dest port
    layout.addWidget(textEdit4)

    layout.addWidget(label5) #flags
    layout.addWidget(flag1)
    layout.addWidget(flag2)
    layout.addWidget(flag3)
    layout.addWidget(flag4)
    layout.addWidget(flag5)
    layout.addWidget(flag6)
    layout.addWidget(flag7)
    layout.addWidget(flag8)


    layout.addWidget(label6)
    layout.addWidget(textEdit6)

    runCommandButton = QPushButton("Send Packet")
    runCommandButton.setObjectName("runCommandButton")

    vbox1.addLayout(layout)
    vbox1.addWidget(runCommandButton)
    #QtCore.QObject.connect(runCommandButton, QtCore.SIGNAL("clicked()"), createScapyCommand)

    widget.setLayout(vbox1)
    widget.show()
    sys.exit(app.exec_())


buildTCPPanal()

1 个答案:

答案 0 :(得分:1)

您必须在放置QScrollArea的位置创建一个新窗口小部件,并显示该新窗口小部件。将PyQt4PySide的元素组合在一起也不合适。另一个不好的做法是在函数中使用超出该范围的元素。

from PyQt4 import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)


def buildTCPPanal():
    widget = QtGui.QWidget()
    layout = QtGui.QGridLayout()
    vbox1 = QtGui.QVBoxLayout()

    button = QtGui.QPushButton("Send")

    flag1 = QtGui.QCheckBox("CWR")
    flag2 = QtGui.QCheckBox("ECN")
    flag3 = QtGui.QCheckBox("URG")
    flag4 = QtGui.QCheckBox("ACK")
    flag5 = QtGui.QCheckBox("PSH")
    flag6 = QtGui.QCheckBox("RST")
    flag7 = QtGui.QCheckBox("SYN")
    flag8 = QtGui.QCheckBox("FIN")

    textEdit1 = QtGui.QTextEdit('')
    textEdit2 = QtGui.QTextEdit('')
    textEdit3 = QtGui.QTextEdit('')
    textEdit4 = QtGui.QTextEdit('')
    #textEdit5 = QtGui.QTextEdit('')#using check boxes instead
    textEdit6 = QtGui.QTextEdit('')

    label1 = QtGui.QLabel("IP Destanation Address")
    label2 = QtGui.QLabel("IP Source Address")
    label3 = QtGui.QLabel("Source Port")
    label4 = QtGui.QLabel("Destanation Port")
    label5 = QtGui.QLabel("Flags")
    label6 = QtGui.QLabel("Data")

    layout.addWidget(label1) #dest add
    layout.addWidget(textEdit1)

    layout.addWidget(label2) #src add
    layout.addWidget(textEdit2)

    layout.addWidget(label3) #src port
    layout.addWidget(textEdit3)

    layout.addWidget(label4) #dest port
    layout.addWidget(textEdit4)

    layout.addWidget(label5) #flags
    layout.addWidget(flag1)
    layout.addWidget(flag2)
    layout.addWidget(flag3)
    layout.addWidget(flag4)
    layout.addWidget(flag5)
    layout.addWidget(flag6)
    layout.addWidget(flag7)
    layout.addWidget(flag8)


    layout.addWidget(label6)
    layout.addWidget(textEdit6)

    runCommandButton = QtGui.QPushButton("Send Packet")
    runCommandButton.setObjectName("runCommandButton")

    vbox1.addLayout(layout)
    vbox1.addWidget(runCommandButton)
    #QtCore.QObject.connect(runCommandButton, QtCore.SIGNAL("clicked()"), createScapyCommand)

    widget.setLayout(vbox1)
    widget.show()

    w = QtGui.QWidget()
    layout = QtGui.QVBoxLayout(w)
    scroll = QtGui.QScrollArea()

    scroll.setWidget(widget)
    scroll.setWidgetResizable(True)
    scroll.setFixedHeight(400)
    layout.addWidget(scroll) 
    w.show()

    sys.exit(app.exec_())


buildTCPPanal()

enter image description here