1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
import sys from PyQt4.QtCore import * from PyQt4.QtGui import *
class window(QWidget): def __init__(self, parent=None): super(window, self).__init__(parent) self.init()
def init(self):
hbox = QHBoxLayout() self.leftlist = QListWidget() self.Stack = QStackedWidget() self.toplayout = QHBoxLayout() self.smallbtn = QLabel(u"最小化") self.smallbtn.setObjectName("smallbtn") self.bigbtn = QLabel(u"最大化") self.closebtn = QLabel(u"关闭") self.smallbtn.setAlignment(Qt.AlignRight) self.bigbtn.setAlignment(Qt.AlignRight) self.closebtn.setAlignment(Qt.AlignRight) self.leftlist.insertItem(0,"Contact") self.leftlist.insertItem(1,"Personal") self.leftlist.insertItem(2,"Educational") self.stack1 = QWidget() self.stack2 = QWidget() self.stack3 = QWidget() self.stack1Window() self.stack2Window() self.stack3Window() self.leftlist.currentRowChanged.connect(self.display)
self.toplayout.addWidget(self.smallbtn) self.toplayout.addWidget(self.bigbtn) self.toplayout.addWidget(self.closebtn) self.Stack.addWidget(self.stack1) self.Stack.addWidget(self.stack2) self.Stack.addWidget(self.stack3) hbox.addWidget(self.leftlist) hbox.addWidget(self.Stack) mainlayout = QVBoxLayout(self) mainlayout.addLayout(self.toplayout) mainlayout.addLayout(hbox) self.setLayout(mainlayout) self.setWindowTitle(u"关联标签显示") file = QFile('test.qss') file.open(QFile.ReadOnly) styleSheet = file.readAll() styleSheet = unicode(styleSheet, encoding='utf8') self.setStyleSheet(styleSheet) self.setMouseTracking(True) desktop = QApplication.desktop() width = desktop.width() height = desktop.height() if (width/height) <3 : self.move((width - self.width())/2, (height - self.height())/2) self.setWindowFlags(Qt.FramelessWindowHint)
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.m_drag = True self.m_DragPosition = event.globalPos() - self.pos() event.accept()
def mouseMoveEvent(self, QMouseEvent): if QMouseEvent.buttons() and Qt.LeftButton: self.move(QMouseEvent.globalPos() - self.m_DragPosition) QMouseEvent.accept()
def mouseReleaseEvent(self, QMouseEvent): self.m_drag = True
def stack1Window(self): layout = QFormLayout() layout.addRow("Name", QLineEdit()) layout.addRow("Address", QLineEdit()) self.stack1.setLayout(layout)
def stack2Window(self): layout = QFormLayout() sex = QHBoxLayout() sex.addWidget(QRadioButton("Male")) sex.addWidget(QRadioButton("Female")) layout.addRow(QLabel("Sex"), sex) layout.addRow("Date of Birth", QLineEdit())
self.stack2.setLayout(layout)
def stack3Window(self): layout = QHBoxLayout() layout.addWidget(QLabel("subjects")) layout.addWidget(QCheckBox("Physics")) layout.addWidget(QCheckBox("Maths")) self.stack3.setLayout(layout)
def display(self, i): self.Stack.setCurrentIndex(i)
def main(): app = QApplication(sys.argv) ex = window() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|