pyside6 两个页面互相跳转

kuka示教器嵌套UR界面操作ros中rviz的UR机器人-优快云博客

接上一篇,探索了两个页面互相跳转的操作。

1.两个页面

页面:UrWin,主要显示Ur机器人的VNC远程控制界面

页面:ZcWin,主要是选择插针的长度

 在Ur远程界面点击下一步会跳转到针选择界面,在针选择界面点击上一步按钮会跳转到Ur远程界面。而且只保留一个界面。原界面会非销毁掉。

2.界面实现的代码

ur界面参考上一篇博客,针选择界面也是类似的操作一遍即可

2.1 针选择界面

pyside6-uic zhen_choose.ui -o ui_zhen_choose.py

 

 

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'zhen_choose.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QPushButton, QSizePolicy,
    QWidget)

class Ui_Zhen_choose(object):
    def setupUi(self, Zhen_choose):
        if not Zhen_choose.objectName():
            Zhen_choose.setObjectName(u"Zhen_choose")
        Zhen_choose.resize(1050, 747)
        Zhen_choose.setStyleSheet(u"background-image: url(:/kuka_top.png);")
        self.label = QLabel(Zhen_choose)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(370, 320, 101, 17))
        self.label_2 = QLabel(Zhen_choose)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setGeometry(QRect(370, 350, 101, 17))
        self.label_3 = QLabel(Zhen_choose)
        self.label_3.setObjectName(u"label_3")
        self.label_3.setGeometry(QRect(370, 380, 101, 17))
        self.Bt_lastone = QPushButton(Zhen_choose)
        self.Bt_lastone.setObjectName(u"Bt_lastone")
        self.Bt_lastone.setGeometry(QRect(500, 570, 89, 25))

        self.retranslateUi(Zhen_choose)

        QMetaObject.connectSlotsByName(Zhen_choose)
    # setupUi

    def retranslateUi(self, Zhen_choose):
        Zhen_choose.setWindowTitle(QCoreApplication.translate("Zhen_choose", u"\u9009\u62e9\u9488", None))
        self.label.setText(QCoreApplication.translate("Zhen_choose", u"\u94881\uff1a50mm", None))
        self.label_2.setText(QCoreApplication.translate("Zhen_choose", u"\u94882\uff1a60mm", None))
        self.label_3.setText(QCoreApplication.translate("Zhen_choose", u"\u94881\uff1a80mm", None))
        self.Bt_lastone.setText(QCoreApplication.translate("Zhen_choose", u"\u4e0a\u4e00\u6b65", None))
    # retranslateUi

2.1 Ur界面 

 

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'urVnc.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPushButton, QSizePolicy, QWidget)

from PySide6.QtWebEngineWidgets import QWebEngineView
#from QtWebKitWidgets.QWebView import QWebView

class Ui_urVNC(object):
    def setupUi(self, urVNC):
        if not urVNC.objectName():
            urVNC.setObjectName(u"urVNC")
        urVNC.resize(1056, 750)
        urVNC.setStyleSheet(u"background-image: url(:/kuka_top.png);")
        self.Bt_next = QPushButton(urVNC)
        self.Bt_next.setObjectName(u"Bt_next")
        self.Bt_next.setGeometry(QRect(710, 570, 89, 25))
        self.webView = QWebEngineView(urVNC)
        self.webView.setObjectName(u"webView")
        self.webView.setGeometry(QRect(230, 190, 601, 371))
        self.webView.setUrl(QUrl(u"http://192.168.56.101:6080/vnc.html"))
        self.webView.setZoomFactor(0.500000000000000)
        self.Bt_lastone = QPushButton(urVNC)
        self.Bt_lastone.setObjectName(u"Bt_lastone")
        self.Bt_lastone.setGeometry(QRect(580, 570, 89, 25))

        self.retranslateUi(urVNC)

        QMetaObject.connectSlotsByName(urVNC)
    # setupUi

    def retranslateUi(self, urVNC):
        urVNC.setWindowTitle(QCoreApplication.translate("urVNC", u"UR VNC", None))
        self.Bt_next.setText(QCoreApplication.translate("urVNC", u"\u4e0b\u4e00\u6b65", None))
        self.Bt_lastone.setText(QCoreApplication.translate("urVNC", u"\u4e0a\u4e00\u6b65", None))
    # retranslateUi

3.主程序

      这个实现主要是在主程序里面增加几行代码即可,如下图所示,给相关的按钮赋予点击操作,操作函数显示窗口和关闭窗口。

     

    相关按钮的名字可以在相关ui的python文件中找。如下图,这些名字就是这么一 一对应,选择调用即可。

 

# This Python file uses the following encoding: utf-8

# if __name__ == "__main__":
#     pass

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget
from PySide6.QtCore import QFile
from ui_urVnc import Ui_urVNC
from ui_zhen_choose import Ui_Zhen_choose
import images

class UrWin(QMainWindow):
    def __init__(self):
        super(UrWin, self).__init__()
        self.ui = Ui_urVNC()
        self.ui.setupUi(self)
        self.ui.Bt_next.clicked.connect(self.show_ZcWin)

    def show_ZcWin(self, checked):
        print("dian ji")
        self.w = ZcWin()
        self.w.show()
        self.close()


class ZcWin(QMainWindow):
    def __init__(self):
        super(ZcWin, self).__init__()
        self.ui = Ui_Zhen_choose()
        self.ui.setupUi(self)
        self.ui.Bt_lastone.clicked.connect(self.show_urvnc)
    def show_urvnc(self,checked):
        self.win_urvnc = UrWin()
        self.win_urvnc.show()
        self.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = UrWin()
    window.show()

    sys.exit(app.exec())

 4.运行测试

所需要的文件放到一个文件里面就可以了,

就是这些文件,就可以正常的运行了

### 实现 PySide6 中按钮点击事件触发页面切换 在 PySide6 应用程序中,可以通过连接按钮的 `clicked` 信号到自定义槽函数来实现页面之间的切换。下面是一个完整的例子,展示了如何创建多个窗口并使用按钮控制它们之间相互切换。 #### 创建主窗口和目标窗口类 首先定义两个不同的窗口类——一个是作为应用程序起点的主要窗口;另一个是要跳转的目标窗口: ```python from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Main Window") layout = QVBoxLayout() # Create a button to navigate away from this window. switch_button = QPushButton("Go To Next Page") switch_button.clicked.connect(self.go_to_next_page) layout.addWidget(switch_button) container = QWidget() container.setLayout(layout) setCentralWidget(container) def go_to_next_page(self): """Slot function that handles the transition logic.""" next_window = SecondaryWindow() next_window.show() # Hide current main window after showing new one. hide() class SecondaryWindow(QMainWindow): def __init__(self): super().__init__() setupUi(this) # Assuming there's an associated .ui file or similar method. setWindowTitle("Secondary Window") if __name__ == "__main__": app = QApplication([]) mainWindow = MainWindow() mainWindow.show() exit(app.exec()) ``` 此代码片段展示了一个简单的两页应用结构,在其中包含了用于启动新窗口实例的方法[^4]。当用户按下 "Go To Next Page" 按钮时,会调用 `go_to_next_page()` 方法,该方法负责显示新的窗口并将当前窗口隐藏起来。 为了使这个过程更加流畅,还可以考虑采用堆栈式布局管理器 (`QStackedLayout`) 或者卡片式的视图组件(`QTabWidget`, `QWizard`), 这样可以在同一个父容器内平滑过渡不同页面而不需要频繁地创建销毁窗口对象[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leecheni

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值