注意:我已经阅读了这篇文章,不幸的是我没有理解它。我的目录设置有点像这样:
Main_Folder
|_ Base_Gui_File.py
|_ Child_directory (a directory inside Main_Folder)
|_ __init__.py
|_ some_other.py
我已经在Base_Gui_File.py
文件中获得了所有 GUI 代码,这些代码是从designer
(PyQt4) 生成的。有一个文本输入字段QLineEdit
、一个按钮QPushButton
和一个文本区域QTextBrowser
。
默认QTextBrowser
是隐藏的。但是,我想做的是,当有人在中键入内容QLineEdit
并单击时QPushButton
,它会将字符串 from 发送QLineEdit
到some_other.py
文件中的方法Child_Directory
. 在使用该字符串执行某些操作后,some_other.py
文件中的方法将show
在QTextBrowser
中Base_Gui_File.py
并在QTextBrowser
.
到目前为止,我已经能够Base_GUI_File.py
通过some_other.py
从QLineEdit
. 这是两个文件的代码:
some_other.py:
import sys
sys.path.append("..")
from Base_Gui_File import Ui_MainWindow
class childone(object):
"""docstring for childone"""
def __init__(self):
super(childone, self).__init__()
def somemethod(self, url):
pass
print 'Hey!'
final_string = str(url) + "Just tying this!"
print final_string
Base_Gui_file.py:
# -*- coding: utf-8 -*-
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, 544)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.MyPushButton = QtGui.QPushButton(self.centralwidget)
self.MyPushButton.setGeometry(QtCore.QRect(680, 40, 75, 23))
self.MyPushButton.setObjectName(_fromUtf8("MyPushButton"))
self.MyLabel = QtGui.QLabel(self.centralwidget)
self.MyLabel.setGeometry(QtCore.QRect(30, 30, 46, 13))
self.MyLabel.setObjectName(_fromUtf8("MyLabel"))
self.MyTextArea = QtGui.QTextBrowser(self.centralwidget)
self.MyTextArea.setGeometry(QtCore.QRect(20, 110, 721, 361))
self.MyTextArea.setObjectName(_fromUtf8("MyTextArea"))
self.MyTextField = QtGui.QLineEdit(self.centralwidget)
self.MyTextField.setGeometry(QtCore.QRect(100, 30, 571, 41))
self.MyTextField.setObjectName(_fromUtf8("MyTextField"))
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.menubar.addAction(self.menuFIle.menuAction())
self.MyPushButton.clicked.connect(self.download_click)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.MyPushButton.setText(_translate("MainWindow", "PushButton", None))
self.MyLabel.setText(_translate("MainWindow", "TextLabel", None))
self.menuFIle.setTitle(_translate("MainWindow", "FIle", None))
def download_click(self):
self.MyTextArea.textCursor().insertHtml('Im HERE!') # This is working as it should
url = str(self.MyTextField.text())
from Child_directory.some_other import childone
childone().somemethod(url)
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_())
我该怎么做?两天前我开始了 GUI 和 OO 编程(Python),所以我对它很陌生。所以,即使它只是一个正确方向的指南,那也很棒!