edit = QTextEdit()
edit.setPlainText("\n")多行输入
#信号和槽
def my_click():
print("输出名")
btn = QPushButton("信号名")
btn.setParent(window)
btn.clicked.connect(my_click)
#水平竖直布局:
layout = QHBoxLayout()...............................水平是H horizontal 竖直是V vertical
window.setLayout(layout).............................补充:layout 布局,argument 参数
#表单布局
layout = QFormLayout()
window.setLayout(layout)
row1 = QLineEdit()
layout.addRow("姓名",row1)
row2 = QLineEdit()
layout.addRow("年龄", row2)
row3 = QLineEdit()
layout.addRow("地址", row3)
bnt1 = QPushButton("发送")
layout.addRow(bnt1)//该函数可输出一个,也可输出两个
通过btn.clicked.connect(my_click)连接函数:可以print(row1.text())
#表格布局
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
if __name__ == '__main__':
#创建app
app =QApplication(sys.argv)
#窗体显示
window = QWidget()
layout = QGridLayout()
window.setLayout(layout)
for i in range(4):
for j in range(4):
btn = QPushButton("按钮{}{}".format(i,j))
layout.addWidget(btn,i,j)
window.show()
sys.exit(app.exec())
#QMessageBox
QMessageBox.information(window,"title","content")#对话框信息
QMessageBox.question(window,"title",content")#问答框,会弹出yes no
btn1 = QPushButton("问答")
btn1.setParent(window)
btn1.clicked.connect(my_click)#时间绑定->按钮通过这个触发函数
#Qinpuydialog
text,ok=QInputDialog.getText(window,"title",'content')#返回值为一元祖,
if ok:
print(text)
#单选框
btn1 = QRadioButton("是")
btn2 = QRadioButton("否")
btn1.setChecked(True)#设置默认选择
layout.addWidget(btn1)
layout.addWidget(btn2)
#多选框
label = QLabel("生肖")
btn1 = QCheckBox("鼠")
btn2 = QCheckBox("牛")
btn3 = QCheckBox("虎")
btn4 = QCheckBox("兔")
layout.addWidget(label)
layout.addWidget(btn1)
layout.addWidget(btn2)
layout.addWidget(btn3)
layout.addWidget(btn4)
#类:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
def my_click():
print("内容如下:")
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
layout = QHBoxLayout()
self.setLayout(layout)
btn1 = QPushButton("push")
layout.addWidget(btn1)
btn1.clicked.connect(my_click)
if __name__ == '__main__':
# 创建app
app = QApplication(sys.argv)
# 窗体显示
window =MyWindow()//把QWidget改成 MyWindow
window.show()
sys.exit(app.exec())
python中两条下划线表示私有化:
class Person: def __init__(self,name): self.__name=name if __name__ == '__main__': person=Person("jack") print(person.name) //此程序报错