ありがとう 发表于 2020-4-9 22:53:59

请问为什么调用不了另一个py文件的函数呢 也用了from import了

这样字按钮点开后就会崩溃 函数不能导入

y1.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget,QApplication,QTextEdit,QVBoxLayout,QLabel
from PyQt5.QtGui import QDragEnterEvent,QPixmap
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

from y2 import *

class Example(QWidget):
   
    def __init__(self):
      super().__init__()
      self.initUI()
    def initUI(self):
      self.resize(640, 320)

      self.bt = QPushButton('按钮',self)
      self.bt.setGeometry(200, 150, 200, 121)
      self.bt.clicked.connect(self.zyq)

    def zyq(self):
            y2.syq()








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

y2.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget,QApplication,QTextEdit,QVBoxLayout,QLabel
from PyQt5.QtGui import QDragEnterEvent,QPixmap
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example2(QWidget):
   
    def __init__(self):
      super().__init__()
      self.initUI()
    def initUI(self):
      self.resize(640, 320)


      
    def xyq(self):
      print('python')








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

小小小菜菜菜 发表于 2020-4-9 23:00:54

你使用了from y2 import * 之后def zyq(self):
            y2.syq()这个方法就不对,直接使用xyq()

zltzlt 发表于 2020-4-10 08:01:45

    def zyq(self):
            y2.syq()

这一段改成这样试试:

    def zyq(self):
            syq()

永恒的蓝色梦想 发表于 2020-4-10 08:09:10

class Example(QWidget):
   
    def __init__(self):
      super().__init__()
      self.initUI()
    def initUI(self):
      self.resize(640, 320)

      self.bt = QPushButton('按钮',self)
      self.bt.setGeometry(200, 150, 200, 121)
      self.bt.clicked.connect(self.zyq)

    def zyq(self):
      return Example2.xyq(self)
页: [1]
查看完整版本: 请问为什么调用不了另一个py文件的函数呢 也用了from import了