2016-12-21 13 views
3

注:私はすでにthisの投稿を読んでいますが、残念ながらそれを理解していませんでした。 私は多少このようにディレクトリ設定を持っている:私はdesigner(PyQt4)から生成されたBase_Gui_File.pyファイルにすべてのGUIコードを持っているPyQtの外部GUIクラスからGUI要素にアクセスする

Main_Folder 
|_ Base_Gui_File.py 
|_ Child_directory (a directory inside Main_Folder) 
    |_ __init__.py 
    |_ some_other.py 

。テキスト入力フィールドQLineEdit、プッシュボタンQPushButton、テキストエリアQTextBrowserがあります。

デフォルトではQTextBrowserが非表示になっています。しかし、私がしたいことは、QLineEditの何かを入力してQPushButtonをクリックすると、QLineEditの文字列をChild_Directorysome_other.pyファイルのメソッドに送信するということでした。そして、その文字列で何かを実行した後、some_other.pyファイルのメソッドはshowQTextBrowserBase_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_()) 

私はどのように行うことができますか?私は2日前にGUIとOOプログラミング(Python)を始めましたので、私はかなり新しいです。だから、たとえそれがちょうど正しい方向のガイドであったとしても、それは素晴らしいだろう!

答えて

1

あなたがリンクした答えで言ったように:pyuicで生成されたモジュールを編集するのは間違いです。これは、メインプログラムにインポートする静的なモジュールを意味します。

また、プログラムの現在の構造は逆向きです。メインスクリプトはトップレベルになければならず、すべてのモジュールはその下のパッケージになければなりません。これにより、あなた自身のモジュールをインポートするための奇妙な操作を行う必要がなくなります。のパスので、物事をこのように行うことが非常に重要である

if __name__ == '__main__': 

    import sys 
    from package import app 

    sys.exit(app.run()) 

program 
    |_ main.py 
    |_ package 
     |_ __init__.py 
     |_ app.py 
     |_ gui.py 
     |_ utils.py 

あなたmain.pyスクリプトは非常に簡単なものであり、次のようになります。ここでは構造がどのように見えるかですこのスクリプトはsys.pathの最初のエントリになります。つまり、をのプログラムの他のモジュールで実行すると、インポートは常に正しく動作します。私はあなたがアプリモジュールにあなたのGUIモジュールに対して行われた編集内容を移動した

import sys 
from PyQt4 import QtCore, QtGui 
from package.gui import Ui_MainWindow 
from package import utils 

class MainWindow(QtGui.QMainWindow, Ui_MainWindow): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.setupUi(self) 
     self.MyPushButton.clicked.connect(self.download_click) 

    def download_click(self): 
     self.MyTextArea.textCursor().insertHtml('Hello World!') 
     url = str(self.MyTextField.text()) 
     utils.some_func(url) 

def run():  
    app = QtGui.QApplication(sys.argv) 
    window = MainWindow() 
    window.show() 
    return app.exec_() 

注:

appモジュールは、次のようになります。 MainWindowクラスは、Qtデザイナーで追加したすべてのウィジェットを取り込み、クラスインスタンスの属性になります。したがって、プログラムを再配置したら、pyuicを使用してguiモジュールを再生成する必要があります。

+0

大丈夫です!意味あり。これらのヒントをお寄せいただきありがとうございます。奇妙なディレクトリ構造を頻繁に作成しています。 「あなたがプログラムを並べ替えると、pyuicを使ってguiモジュールを再生成する必要があります」ということを少し詳しく説明できますか? GUIを再配置しますか? – Xonshiz

+0

いいえ - 私が示唆した変更を加えたら、新しいGUIモジュールを作るためにデザイナーUIファイルでpyuicを再実行することができます。そのため、あなたはそのファイルを編集しないでください。qtデザイナーで変更するたびにpyuicを再実行する必要があります。pyuicを実行すると、前に行った編集が破棄されます。 – ekhumoro

+0

ねえ、遅れて申し訳ありません。私は試験で忙しかった。私はあなたが言及したように、今私はいくつかの奇妙な問題に遭遇しました(http://stackoverflow.com/questions/41328165/qtextbrowser-not-updating-in-pyqt-python)。しかし、ここに助けてくれてありがとう! – Xonshiz

関連する問題