gpt4 book ai didi

python - 链接按钮以运行 .py 文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:20 25 4
gpt4 key购买 nike

我想要做的是能够通过单击 main.py 上的按钮打开 grid.py。最好保持相同的结构(ui 文件以及主要和网格 python 文件)。谢谢,

因此,我使用 Qt Designer (test-open.ui) 创建了一个简单的 UI 界面,其中仅包含一个按钮:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>424</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>230</x>
<y>160</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

然后我创建了主 .py 文件 (ma​​in.py):

from PyQt5 import QtWidgets, uic

app = QtWidgets.QApplication([])
dlg = uic.loadUi(r"E:\Python\Project\test-open.ui")

dlg.show()
app.exec()

最后,这是我用 WxPython 创建的网格,我想通过单击按钮打开它 (grid.py)

import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):

def __init__(self):
super().__init__()
self.title = 'PyQt5 table - pythonspot.com'
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createTable()

# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)

# Show widget
self.show()

def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0,0)

# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)

@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

最佳答案

你混淆了概念,程序不是一组文件,而是基于类创建的对象之间的交互。因此,在执行任何操作之前,我将假设这些文件具有以下结构:

├── grid.py
├── main.py
└── test-open.ui

另一方面,在 App 类构造函数中,您正在显示窗口,而这不是您想要的,最初窗口必须关闭,因此我将删除该行。进入正题,您必须在 main 中导入网格模块才能创建 App 小部件对象。

网格.py

import sys

from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QWidget,
QAction,
QTableWidget,
QTableWidgetItem,
QVBoxLayout,
)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 table - pythonspot.com"
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createTable()

# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)

def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(1, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(2, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 4, QTableWidgetItem("Cell (1,1)"))

self.tableWidget.setItem(3, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0, 0)

# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)

@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(
currentQTableWidgetItem.row(),
currentQTableWidgetItem.column(),
currentQTableWidgetItem.text(),
)


if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

主.py

from PyQt5 import QtWidgets, uic
from grid import App


if __name__ == "__main__":
import os
import sys

app = QtWidgets.QApplication(sys.argv)
file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test-open.ui"
)
dlg = uic.loadUi(file)
w = App()
dlg.pushButton.clicked.connect(w.show)
dlg.show()
sys.exit(app.exec_())

关于python - 链接按钮以运行 .py 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56214238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com