在 Python 中向狭窄的 QWidget 添加 QScrollBar
Add a QScrollBar to cramped QWidget in Python
更新上一个问题。
显示时看起来很局促,请参见下图。
我想让 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()
您必须在放置 QScrollArea
的位置创建一个新的小部件,并显示该新的小部件。将 PyQt4
的元素与 PySide
的元素组合起来也不好。另一个不好的做法是在函数中使用该范围之外的元素。
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()
更新上一个问题。
显示时看起来很局促,请参见下图。
我想让 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()
您必须在放置 QScrollArea
的位置创建一个新的小部件,并显示该新的小部件。将 PyQt4
的元素与 PySide
的元素组合起来也不好。另一个不好的做法是在函数中使用该范围之外的元素。
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()