Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learning #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@


class MyWidget(QtWidgets.QWidget):
"""__init__ 方法将任何位置参数收集到 args 元组中,
并将任何关键字参数收集到 kwargs 字典中,
然后将它们作为对象的属性(self)进行赋值。"""

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs) # 调用父类的初始化方法

self.hello = ["你好世界", "Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"]
Expand All @@ -31,7 +36,7 @@ def __init__(self, *args, **kwargs):
self.button.clicked.connect(self.magic) # type: ignore

@QtCore.Slot()
def magic(self) -> None:
def magic(self) -> None: # 设定隐式返回值
"""槽函数"""
self.text.setText(random.choice(self.hello)) # 从列表中随机显示一条问候语

Expand Down
1 change: 1 addition & 0 deletions 01-HelloWorld-基本结构/03-类的继承关系.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def get_sub_classes(class_):
"""递归地显示某一类的所有子类"""
for subclass in class_.__subclasses__():
print(subclass)

if len(subclass.__subclasses__()) > 0:
get_sub_classes(subclass)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setup_ui(self) -> None:
"""设置界面"""

# 创建一个名为red的QWidget
red = QtWidgets.QWidget(self) # red的父控件为self,故不成为单独窗口
red = QtWidgets.QWidget(self) # reSd的父控件为self,故不成为单独窗口
red.resize(100, 100)
red.setStyleSheet("background-color: red;")
red.move(300, 100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_01(self) -> None:
self.label = QtWidgets.QLabel("只是一个Label标签", self) # 创建一个标签子控件
self.label.move(100, 200) # 相对父控件(窗口)移动位置

self.setGeometry(200, 200, 600, 400) # 可以同时设置其位置与尺寸,计算像素时不包含框架
self.setGeometry(200, 200, 600, 600) # 可以同时设置其位置与尺寸,计算像素时不包含框架

def test_02(self) -> None:
"""测试获取位置功能"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setup_ui(self) -> None:
label.setStyleSheet("background-color: cyan; font-size: 30px;")

# 设置内容边距,距离左上角100 200像素
label.setContentsMargins(100, 200, 0, 0)
label.setContentsMargins(0, 200, 0, 0)

# 获取边距
print(label.contentsMargins())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def setup_ui(self) -> None:
self.label_1.move(150, 50)
self.label_2 = QtWidgets.QLabel() # 创建时未指定父控件
self.label_2.setPixmap(QtGui.QPixmap("../../Resources/Icons/Python_128px.png"))
self.label_2.move(100, 150)
self.label_2.setParent(self) # 指定父控件

def test_01(self) -> None:
Expand All @@ -41,7 +42,7 @@ def test_01(self) -> None:

# 获取处于制定坐标的子控件
# 注意:若该坐标无子控件则返回None、似乎对布局管理器无效
print(self.childAt(150, 55))
print(self.childAt(150, 50))

# 以上方法返回的子控件都可以被操作
self.childAt(150, 55).setStyleSheet("background-color: cyan;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def __init__(self):

def setup_ui(self) -> None:
"""设置界面"""
self.label = QtWidgets.QLabel("标签", self)
self.label = QtWidgets.QLabel("标签", self) # 指定父组件为: MyWidget class
self.label.resize(200, 200)
self.label.setStyleSheet("background-color: cyan;")
self.label.setStyleSheet("background-color: #00ff00;") # 设置背景颜色
self.label.move(50, 50)

self.text_edit = QtWidgets.QTextEdit(self)
Expand All @@ -46,7 +46,7 @@ def setup_ui(self) -> None:
def test_01(self) -> None:
"""测试可见与不可见"""

@QtCore.Slot()
@QtCore.Slot() # 显示声明这是一个槽函数
def test_slot() -> None:
if self.label.isVisible():
self.label.setVisible(False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def setup_ui(self) -> None:
def test_01(self):
"""测试设置焦点功能"""
# self.button.clicked.connect(self.pte.setFocus) # type: ignore
self.button.clicked.connect(self.focusNextChild) # type: ignore
self.button.clicked.connect(self.focusNextChild) # type: ignore 使用按键触发切换焦点
self.button.clicked.connect(lambda: print(self.button.hasFocus())) # type: ignore
self.button.clicked.connect(lambda: print(self.le.hasFocus())) # type: ignore
self.button.clicked.connect(lambda: print(self.focusWidget())) # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ def test_01(self) -> None:
"""测试设置光标"""

# 设置为自定义图案光标

pixmap = QtGui.QPixmap("../../Resources/Icons/snowflake_128px.ico").scaled(52, 52)
my_cursor = QtGui.QCursor(
pixmap, 26, 26
) # 以图片像素点位置26,26为热点(光标实际所在位置坐标)
self.setCursor(my_cursor)

# 设置label中的光标为Qt内置的其他光标
self.label.setCursor(Qt.ForbiddenCursor)
self.label.setCursor(Qt.ForbiddenCursor) # 设置label中的光标
# self.label.setCursor(Qt.OpenHandCursor)

def test_02(self) -> None:
"""测试获取光标"""

current_cursor = self.cursor()
self.button.clicked.connect(lambda: print(current_cursor.pos())) # type: ignore
self.button.clicked.connect(lambda: print(current_cursor.pos())) # type: ignore 设置鼠标点击事件


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setup_ui(self) -> None:
QtWidgets.QMessageBox.StandardButton.Ok
) # 添加标准按钮,返回值为按钮实例
# 如果有按钮被按下,则将该按钮的文本打印到终端。
message_box.buttonClicked.connect(lambda btn: print(btn.text())) # type: ignore
message_box.buttonClicked.connect(lambda b: print(b.text())) # type: ignore

# 在主界面上添加一个弹出对话框的按钮,本节功能演示在对话框窗口中而非主窗口中呈现
pop_btn = QtWidgets.QPushButton("弹出对话框", self)
Expand All @@ -77,6 +77,7 @@ def test(self):
"""测试按钮默认与自动默认功能"""
self.ok_btn.setDefault(True)
self.ok_btn.setAutoDefault(True)
# 目前没有看出这俩的区别
print(f"ok_btn.isDefault({self.ok_btn.isDefault()})")
print(f"cancel_btn.isDefault({self.cancel_btn.isDefault()})")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setup_ui(self) -> None:

def test_01(self) -> None:
"""测试按钮组"""
# 将单选按钮添加进按钮组
gender_group = QtWidgets.QButtonGroup(self) # 创建按钮组
gender_group.addButton(self.rb_male) # 将按钮添加至按钮组
gender_group.addButton(self.rb_female) # 处于同一组内的按钮间有互斥性
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_slot(state: int) -> None:
elif state == Qt.PartiallyChecked.value:
print("复选框被部分选中!")

self.cb.stateChanged.connect(test_slot) # type: ignore
self.cb.stateChanged.connect(test_slot)


if __name__ == "__main__":
Expand Down