我想调用另一个def内部的def示例:
def 1(a,b):
Print(a,b)
Def 2 (c,d):
Print(c,d)
我想做的事情就像我们从这样的类中调用一个函数
class().def(variables)
我想做一些类似上面代码的事情类似的东西
def1().def2(variables)
感谢:D
我有这个代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from io import StringIO
from typing import Text
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTime, QTimer
import sys
import time
from PyQt5 import QtTest
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.Console = QtWidgets.QTextEdit()
self.Console.setGeometry(QtCore.QRect(240, 350, 461, 121))
self.Console.setObjectName("Console")
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton1.setGeometry(QtCore.QRect(260, 160, 341, 111))
self.pushButton1.setObjectName("pushButton1")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def printer (text):
sys.stdout = StringIO()
print(text)
time.sleep(0.05)
self.Console.setText(sys.stdout.getvalue())
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton1.setText(_translate("MainWindow", "PushButton"))
Ui_MainWindow.printer("hi")
QtTest.QTest.qWait(5000)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我需要调用打印机功能,但我不能
我试图将q文本对象设置为全局对象,但出现错误print hi命令不起作用,因为打印机fumc在另一个功能上
除非返回,否则不能在另一个函数中调用函数。
这意味着,你不能这样做:
def func1(a,b):
print(a,b)
def func2 (c,d):
print(c,d)
func1().func2()
然而,你能做的是:
def func1(a,b):
print(a,b)
def func2 (c,d):
print(c,d)
return func2
func2 = func1()
func2()
至于你为什么要这样做,我不清楚,但如果你必须这样做,这会让你这样做。
如果可能的话,我建议把两者放在一个班里。