九月 10

SimpleITK无法调用ImageJ/Fiji的问题

原因猜测:因为地址中有一个空格,所以导致无法识别。

测试经过:尝试过给路径加引号,但是并不成功

解决方法:

  • 第一步:在环境变量中新建变量,变量名为SITK_SHOW_COMMAND,变量值为D:\Program Files\Fiji.app(修改为自己的目录)

  • 第二步:在path中添加%SITK_SHOW_COMMAND%

  • 第三步:重启Jupyter notebook就生效

解决方案引用自:https://blog.csdn.net/qq_32384313/article/details/80234732

八月 25

QObject计时器案例(二)

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

class MyWidget(qtw.QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(100, 100)
        self.timer_id = None

    def start_timer(self, time):
        self.timer_id = self.startTimer(time)

    # 重写timerEvent方法
    def timerEvent(self, event):
        current_width = self.width()
        current_height = self.height()
        current_width += 10
        current_height += 10
        self.resize(current_width, current_height)
        if current_width > 500 or current_height > 500:
            self.killTimer(self.timer_id)

app = qtw.QApplication()

window = MyWidget()
window.start_timer(500)

window.show()
sys.exit(app.exec_())
八月 25

QObject计时器案例

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

class MyLabel(qtw.QLabel):

    def __init__(self, *args, **kwargs):
        super(MyLabel, self).__init__(*args, **kwargs)
        self.setText('10')
        self.move(225, 225)
        self.setStyleSheet('font-size: 30px;')
        self.label_timer_id = self.startTimer(1000)

    # 重写timerEvent方法
    def timerEvent(self, event):
        current_time = int(self.text())
        current_time -= 1
        self.setText(str(current_time))

        if current_time == 0:
            self.killTimer(self.label_timer_id)

app = qtw.QApplication()

window = qtw.QWidget()
window.resize(500, 500)

label = MyLabel(window)

window.show()
sys.exit(app.exec_())
八月 25

QObject计时器

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

class MyObject(qtc.QObject):

    # 重写timerEvent方法,就是当定时器经过指定时间后执行的方法
    def timerEvent(self, event):
        print(event, 1)

app = qtw.QApplication()

window = qtw.QWidget(None)
window.resize(800, 800)

obj = MyObject()
# 开始定时器,设置每1000ms执行一次timerEvent方法
# timer_id可以用来killTimer(timer_id)
# startTimer方法的第二个参数为设置计时器的精度
timer_id = obj.startTimer(1000, qtc.Qt.CoarseTimer)

# 关闭timer_id对应的计时器
obj.killTimer(timer_id)

window.show()

sys.exit(app.exec_())
八月 25

Qt的事件机制(一)

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

def pressed_slot():
    print('按钮被点击了-slot')

# 为了查看QApplication.notify(receiver, event)的方法
# 我们进行方法的override
class App(qtw.QApplication):

    # arg__1为receiver,arg__2为event
    def notify(self, arg__1, arg__2):
        # 判断receiver为QPushButton的子类,同时判断event的类型为qtc.QEvent.MouseButtonPress
        if arg__1.inherits('QPushButton') and arg__2.type() == qtc.QEvent.MouseButtonPress:
            print('receiver: ', arg__1)
            print('event: ', arg__2)

        return super(App, self).notify(arg__1, arg__2)

# 为了查查看receiver.event(event)方法
# 我们进行方法的override
class Btn(qtw.QPushButton):

    # e 为接收的event
    def event(self, e):
        # 注意判断event的类型需要用event.type()方法
        if e.type() == qtc.QEvent.MouseButtonPress:
            print('按钮被点击了-receiver.event')

        return super(Btn, self).event(e)

    # 由event方法分发的具体事件处理方法
    # 信号就在这些具体事件处理方法中发射
    def mousePressEvent(self, e):
        print('鼠标被按下了-具体的分发函数')

        return super(Btn, self).mousePressEvent(e)

app = App()

window = qtw.QWidget()

btn = Btn(window)
btn.setText('点我')
btn.move(100, 100)

btn.pressed.connect(pressed_slot)

window.show()

sys.exit(app.exec_())
八月 24

QObject对象删除

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

app = qtw.QApplication()

obj1 = qtc.QObject(None)
obj2 = qtc.QObject(obj1)
obj3 = qtc.QObject(obj2)

obj1.destroyed.connect(lambda: print('obj1被释放了'))
obj2.destroyed.connect(lambda: print('obj2被释放了'))
obj3.destroyed.connect(lambda: print('obj3被释放了'))

# 直接使用del进行删除,如果这个对象有父对象,则删除不了
# 但是用deleteLater方法就可以将有父对象的对象直接删除
obj2.deleteLater()
# deleteLater不会立即执行,而是要等到下一个消息循环再删除
print(obj1.children())

sys.exit(app.exec_())
八月 24

QObject类型判定案例

import sys
from PySide2 import QtWidgets as qtw

app = qtw.QApplication()

window = qtw.QWidget()
label_1 = qtw.QLabel(window)
label_1.setText('安静一键宏')

label_2 = qtw.QLabel(window)
label_2.setText('轻松玩魔兽')
label_2.move(0, 30)

btn = qtw.QPushButton(window)
btn.setText('点我')
btn.move(0, 60)

# .children() 方法只是返回所有的子对象,没有findChildren的过滤功能
for item in window.children():
    if item.inherits('QLabel'):
        item.setStyleSheet('background-color: red;')

window.show()

sys.exit(app.exec_())
八月 24

QObject类型判定

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

app = qtw.QApplication()

obj = qtc.QObject()
widget = qtw.QWidget()
btn = qtw.QPushButton()
label = qtw.QLabel()

items = [obj, widget, btn, label]

for item in items:

    print('============================')
    # 判断对象是否为控件(widget)类型
    print('是否为控件:', item.isWidgetType())
    # inherits包含了直接继承和间接继承
    # inherts接收的参数为类名,如下所示:
    print('是否继承自QWidget:', item.inherits('QWidget'))
    print('是否继承自QPushButton:', item.inherits('QPushButton'))
    print('============================')

sys.exit(app.exec_())
八月 24

QObject信号与槽案例

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

def window_title_changed_slot(name):
    # 暂时屏蔽所有的信号,防止出现递归调用
    window.blockSignals(True)
    window.setWindowTitle('安静一键宏 - ' + name)
    # 解除信号屏蔽
    window.blockSignals(False)

app = qtw.QApplication()

window = qtw.QWidget(None)
window.resize(800, 800)
window.windowTitleChanged.connect(window_title_changed_slot)
# 触发信号,执行槽函数
window.setWindowTitle('猎人')

# 每次点击的时候print('点你咋地!')
btn = qtw.QPushButton(window)
btn.setText('点我')
btn.clicked.connect(lambda: print('点你咋地!'))

window.show()
sys.exit(app.exec_())
八月 24

QObject信号与槽

import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtCore as qtc

# destroy信号会携带一个参数,这个参数就是被destroy的对象
def name_changed_slot(name):
    print(f'对象名称改变为{name}')

def destroy_slot(obj):
    print(f'{obj}对象被释放了')

app = qtw.QApplication()

window = qtw.QWidget(None)
window.resize(800, 800)

obj = qtc.QObject(None)

# 连接信号与槽
obj.objectNameChanged.connect(name_changed_slot)
obj.destroyed.connect(destroy_slot)

# 检查信号绑定的槽的数量
# 这里的参数要用SIGNAL配合信号名称获取
# 信号名称不用带上sender的名称,但是后面需要加(),否则返回0个receiver
print(obj.receivers(qtc.SIGNAL('destroyed()')))

# 触发objectNameChanged信号
obj.setObjectName('changed_name')
# 临时阻断信号
obj.blockSignals(True)
# obj.signalsBlocked返回布尔值,检测信号是否被阻断
print(obj.signalsBlocked())
obj.setObjectName('another_changed_name')
# 恢复信号连接
obj.blockSignals(False)

# 取消信号与槽的链接。如果不传参数,测取消所有链接。也可以指定取消链接的槽
obj.objectNameChanged.disconnect()
obj.setObjectName('third_changed_name')

# 删除引用,即触发obj的destroy信号
del obj

window.show()

sys.exit(app.exec_())