发布于2024-11-06 19:35 阅读(282) 评论(0) 点赞(5) 收藏(2)
我有以下 2 个类。从中我打开一个对话框,如下所示。我希望当我单击这两个按钮之一( , )UI_mainwindow
时关闭该对话框。虽然似乎什么都没起作用。createProject_btn
openProject_btn
我知道这类问题已经被问过无数次了,而且我确信肯定有重复的问题,但我对 Python 还很陌生,无法理解其中的规律。因此,任何帮助我都感激不尽。
注意:ui 文件非常简单,UI_mainwindow 上有一个按钮用于打开对话框,对话框上有两个按钮用于在内部设置属性并关闭对话框。
用户界面主窗口
import os
from .createOrOpenProjectDialog import CreateOrOpenProjectDialog
from qgis.PyQt import uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QDialog)
FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui', 'window.ui'))
class Ui_mainWindow(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(Ui_mainWindow, self).__init__(parent)
# Set up the user interface from Designer through FORM_CLASS.
self.setupUi(self)
self.connectSignalsSlots()
def connectSignalsSlots(self):
print("main.connectSignalsSlots 1")
self.pushButton1.clicked.connect(self.createProject)
print("main.connectSignalsSlots 2")
def createProject(self):
dialog = MyDialog(self)
print(dialog.exec())
dialog.showOutput()
class MyDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
# .dialog is just a handle - class property that we created here
self.dialog = CreateOrOpenProjectDialog(self)
def showOutput(self):
print("showOutput:")
print(self.dialog.clickedButton)
创建或打开项目对话框
import os
from qgis.PyQt import uic
from PyQt5.QtWidgets import (QDialog)
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui', 'dialog.ui'))
class CreateOrOpenProjectDialog(QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(CreateOrOpenProjectDialog, self).__init__(parent)
self.setupUi(parent)
self.connectSignalsSlots()
def connectSignalsSlots(self):
# todo: figure out if actions are better to use then direct events
# self.action_create_project.triggered.connect(self.createProject)
self.createProject_btn.clicked.connect(self.createProject)
self.openProject_btn.clicked.connect(self.openProject)
def createProject(self):
print("dialog.createProject")
self.clickedButton = "createProj"
self.accept()
def openProject(self):
print("dialog.openProject")
self.clickedButton = "openProj"
self.accept()
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2043858/07c4a805f2bf07f010c3/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!