简体   繁体   English

如何从 Python 的主模块中获取变量?

[英]How do I get variables from my main module in Python?

I am a Structural Engineer by trade and I am trying to automate the creation of 3D models using scripts.我是一名结构工程师,我正在尝试使用脚本自动创建 3D 模型。

So far I have created three modules;到目前为止,我已经创建了三个模块; the GUI module using PyQt4, a main module that controls the signals from the GUI, and an export module which "should" pull the variables from main module and generate a script that can be read by my analysis program.使用 PyQt4 的 GUI 模块,控制来自 GUI 的信号的主模块,以及“应该”从主模块中提取变量并生成可由我的分析程序读取的脚本的导出模块。

So far the I can't pull the variables from main module when clicking the export menu in the GUI because variable names are not defined.到目前为止,当单击 GUI 中的导出菜单时,我无法从主模块中提取变量,因为未定义变量名称。

If I import the main module into the export module to get the variables, I get errors with the Ui_MainWindow.如果我将主模块导入导出模块以获取变量,则会出现 Ui_MainWindow 错误。

I have tried to simplify what I am doing below.我试图简化我在下面所做的事情。

main.py module main.py 模块

import sys
from PyQt4 import QtGui, QtCore
from gui import Ui_MainWindow
from export import newFile

class Main(QtGui.QMainWindow):

    def __init__(self):
    super(Main, self).__init__()
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)


    self.setName()

    self.ui.actionExport.triggered.connect(self.exportName)

def exportName(self):
    self.exportStaad = newFile().createNewFile()

def setName(self):
    self.ui.tbo_Name.textChanged.connect(self.name_Changed)

def name_Changed(self):
    someName = self.ui.tbo_Name.text()
    print('Name = ' + someName)


app = QtGui.QApplication(sys.argv)
form = Main()
form.show()
app.exec_()

gui.py gui.py

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

# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tbo_Name = QtGui.QLineEdit(self.centralwidget)
        self.tbo_Name.setGeometry(QtCore.QRect(80, 60, 150, 20))
        self.tbo_Name.setObjectName(_fromUtf8("tbo_Name"))
        self.lab_Name = QtGui.QLabel(self.centralwidget)
        self.lab_Name.setGeometry(QtCore.QRect(30, 60, 40, 20))
        self.lab_Name.setObjectName(_fromUtf8("lab_Name"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.actionExport = QtGui.QAction(MainWindow)
        self.actionExport.setObjectName(_fromUtf8("actionExport"))
        self.menuFile.addAction(self.actionExport)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.lab_Name.setText(_translate("MainWindow", "Name:", None))
        self.menuFile.setTitle(_translate("MainWindow", "File", None))
        self.actionExport.setText(_translate("MainWindow", "Export", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

export.py导出文件

import sys
from PyQt4 import QtGui, QtCore
from os import path
import math

class newFile():
    def createNewFile(dest):
        '''
        Creates file
        '''

        name = QtGui.QFileDialog.getSaveFileName ()

        f = open(name, 'w')
        f.write('Hello' + someName)


        f.close

The method called createNewFile(dest) inside the class newFile uses undefined var someName at f.write('Hello' + someName) .类 newFile 中名为 createNewFile(dest) 的方法在f.write('Hello' + someName) 中使用未定义的 var someName。 This causes the error as it is not defined in the class.这会导致错误,因为它没有在类中定义。 Define a variable before you use it.在使用之前定义一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Go 中使用 Python 模块中的变量? - How do I make variables from a Python module available in Go? 如何将变量从一个函数传递到另一个函数? - How do I get my variables from one function to another? Python Tkinter-如何从主窗口类外部更改变量? - Python Tkinter - How do I change variables from outside the main window class? 如何在Python的另一个模块中解决未初始化的全局变量? - How do I get around uninitialized global variables in another module in Python? 如何在 Python 中将变量从模块传递到模块? - How can I pass variables from module to module in Python? 当我的主要脚本位于整个包的子文件夹中时,如何从python脚本导入? - How do I do imports from a python script when my main scripts are in a subfolder of the whole package? 我需要对我的python代码做什么才能让它成为一个模块? - What do I need to do to my python code to get it to be a module? Python:如何在模块中使用主文件中的变量? - Python: How can I use variable from main file in module? 如何将变量中的偏差值合并到 function 中? [Python] - How do I incorporate the bias values from my variables into my function? [python] 如何获取模块中的所有变量,但不包括在该模块中导入的变量 - How do I get all the variables that are in a module, but excluding the variables that are imported in that module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM