我显示一个窗口,其中有30个以上的Checkbox(基于数据库结果的基础),此代码是静态的,它创建了Checkbox,但不显示窗口上方的大小,我想添加滚动以显示所有Checkbox并选择哪个用户想要,我该怎么做? [这是我们从第一个窗口传递并选择了某些字段之后的第二个窗口,self.main_query是由用户从第一页选择的查询]
import sys
from PyQt5.QtWidgets import QScrollBar, QSlider, QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel, QCheckBox
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot
import pymysql
class FormTwo(QWidget):
def __init__(self):
super().__init__()
self.title = 'Second Page which Showing Analytics'
self.title = 'First Main Window'
self.left = 0
self.top = 0
self.width = 700
self.height = 600
self.main_query = " select data from database where "
self.initform2ui()
self.show()
def initform2ui(self):
conn = pymysql.connect(host = 'localhost', user = 'root', password = '********', db = 'db_name')
cur4 = conn.cursor()
query_for_analytics = "select distinct Analytics from analyticsreport"
cur4.execute(query_for_analytics)
self.no_of_analytics = cur4.rowcount
result = cur4.fetchall()
checkbox = 'checkbox3'
r_move = 95
c_move = 75
myFont = QtGui.QFont()
myFont.setBold(True)
self.label2 = QLabel('Analytics', self)
self.label2.setFont(myFont)
self.label2.setStyleSheet('QLabel {Color:blue}')
self.label2.move(100, 50)
self.layout = QVBoxLayout()
#self.layout.addWidget(self.tableWidget)
self.s1 = QSlider()
self.setLayout(self.layout)
self.setWindowTitle('Proceed for the result of Analytics')
self.setGeometry(self.top, self.left, self.width, self.height)
self.button1 = QPushButton('Proceed For Result', self)
self.button1.setStyleSheet('background-color:darkblue; color: white')
self.button1.move(140,300)
self.button1.clicked.connect(self.on_button_pushed)
self.list_of_checkbox_for_analytics = []
for i in range(self.no_of_analytics):
name = str(list(result[i]))
print("name", name)
name = name.replace("[", "")
name = name.replace("]", "")
name = name.replace("'", "")
cb1 = checkbox + str(i)
self.list_of_checkbox_for_analytics.append(name)
self.list_of_checkbox_for_analytics[i] = QCheckBox(name, self)
self.list_of_checkbox_for_analytics[i].adjustSize()
self.list_of_checkbox_for_analytics[i].move(r_move, c_move)
c_move = c_move + 20
def on_button_pushed(self):
initialize_ai = 0
flag = 0
ana_query = ''
for i in range(self.no_of_analytics):
if self.list_of_checkbox_for_analytics[i].isChecked():
print("Checked", self.list_of_checkbox_for_analytics[i].text())
flag = 1
if initialize_ai == 0 and flag == 1:
ana_query = " '" + self.list_of_checkbox_for_analytics[i].text() + "' "
initialize_ai = initialize_ai + 1
flag = 0
if initialize_ai > 0 and flag == 1:
ana_query = ana_query + " or '" + self.list_of_checkbox_for_analytics[i].text() + "' "
flag = 0
if len(ana_query)>2:
ana_query = " and (Analytics = " + ana_query + ")"
main_query = self.main_query + ana_query
else:
main_query = self.main_query
print(main_query)
self.window = QMainWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FormTwo()
sys.exit(app.exec_())
答案 0 :(得分:0)
在这种情况下,您必须使用QScrollArea来处理位置,建议使用布局。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import pymysql
class FormTwo(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.title = 'Second Page which Showing Analytics'
self.left, self.top, self.width, self.height = 0, 0, 700, 600
self.main_query = " select data from database where "
self.initform2ui()
self.show()
def initform2ui(self):
self.setWindowTitle('Proceed for the result of Analytics')
self.setGeometry(self.top, self.left, self.width, self.height)
myFont = QtGui.QFont()
myFont.setBold(True)
self.label2 = QtWidgets.QLabel('Analytics')
self.label2.setFont(myFont)
self.label2.setStyleSheet('QLabel {Color:blue}')
self.button1 = QtWidgets.QPushButton('Proceed For Result')
self.button1.setStyleSheet('background-color:darkblue; color: white')
self.button1.clicked.connect(self.on_button_pushed)
self.list_of_checkbox_for_analytics = []
scrollArea = QtWidgets.QScrollArea()
content_widget = QtWidgets.QWidget()
scrollArea.setWidget(content_widget)
scrollArea.setWidgetResizable(True)
lay = QtWidgets.QVBoxLayout(content_widget)
conn = pymysql.connect(host = 'localhost', user = 'root', password = '********', db = 'db_name')
cur = conn.cursor()
query_for_analytics = "select distinct Analytics from analyticsreport"
cur.execute(query_for_analytics)
for row in cur.fetchall():
name = str(list(row))
name = name.replace("[", "").replace("]", "").replace("'", "")
checkbox = QtWidgets.QCheckBox(name)
checkbox.adjustSize()
lay.addWidget(checkbox)
self.list_of_checkbox_for_analytics.append(checkbox)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label2)
layout.addWidget(self.button1)
layout.addWidget(scrollArea)
@QtCore.pyqtSlot()
def on_button_pushed(self):
initialize_ai = 0
flag = False
ana_query = ''
for checkbox in self.list_of_checkbox_for_analytics:
if checkbox.isChecked():
print("Checked", checkbox.text())
flag = True
if initialize_ai == 0 and flag:
ana_query = " '" + checkbox.text() + "' "
initialize_ai += 1
flag = False
if initialize_ai > 0 and flag:
ana_query += " or '" + checkbox.text() + "' "
flag = False
main_query = self.main_query
if len(ana_query) > 2:
ana_query = " and (Analytics = " + ana_query + ")"
main_query += ana_query
print(main_query)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = FormTwo()
sys.exit(app.exec_())