2017-04-03 4 views
0

以下は、分析用GUIの一部のコードです。メニュー内の国をクリックすると、その国とは異なるメニューを作成する必要があります。しかし、現時点では、どの国が押されていても、最後の国はメニューが作成された国になります(この場合、いずれかの国をクリックすると、オランダのメニューが生成されます)。どうしてこれなの?PyQt - 最後の値を生成するアクション

import csv 
import math 
import itertools 
import numpy as np 
from PyQt4 import QtCore, QtGui 
import matplotlib.pyplot as plt 
from matplotlib.figure import Figure 
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas 

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(QtGui.QMainWindow): 

    def countryAction(self): 

     window = QtGui.QMainWindow(self) 
     window.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
     window.setWindowTitle(self.tr(country)) 
     window.show() 

    def setupUi(self, MainWindow): 
     MainWindow.setGeometry(50, 50, 500, 300) 
     MainWindow.setWindowTitle('uAnalytics') 
     MainWindow.setWindowIcon(QtGui.QIcon('uTalkLogo.png')) 

     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 867, 22)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     self.menuCountry = QtGui.QMenu(self.menubar) 
     self.menuCountry.setObjectName(_fromUtf8("menuCountry")) 
     self.menuPopular_Countries = QtGui.QMenu(self.menuCountry) 
     self.menuPopular_Countries.setObjectName(_fromUtf8("menuPopular_Countries")) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     MainWindow.setStatusBar(self.statusbar) 

     self.actionAll_Countries = QtGui.QAction(MainWindow) 
     self.actionAll_Countries.setObjectName(_fromUtf8("actionAll_Countries")) 
     self.menuCountry.addAction(self.actionAll_Countries) 
     self.menuCountry.addSeparator() 
     self.menuCountry.addAction(self.menuPopular_Countries.menuAction()) 

     continents = {} 
     countries = {} 
     popularcountries = ['United States', 'United Kingdom', 'South Africa', 'Germany', 'India', 'Australia', 'Canada', 'Italy', 'Sweden' ,\ 
    'Netherlands', 'France', 'New Zealand', 'Belgium', 'Switzerland', 'Norway', 'Brazil', 'Indonesia', 'Russia', \ 
    'United Arab Emirates', 'Spain', 'Denmark'] 
     DIR = '/Users/jonathan/Documents/CountryWiseAnalytics/' 
     UsersCountry = {('Europe', 'NL', 'Netherlands'): 231, ('Europe', 'GB', 'United Kingdom'): 2636} 
     for key, value in UsersCountry.iteritems(): 
      continent = key[0] 
      continentMenu = continents.get(continent) 
      if continentMenu is None: 
       continentMenu = self.menuCountry.addMenu(continent) 
       continents[continent] = continentMenu 
      country = key[2] 
      CT = key[1] 
      countryAction = QtGui.QAction(QtGui.QIcon(CT.lower() + '.png'), country, MainWindow) 
      countryAction.setStatusTip('uAnalyse ' + country) 
      countryAction.triggered.connect(lambda: self.countryAction(country)) 
      if country in popularcountries: 
       print(CT.lower() + '.png') 
       self.menuPopular_Countries.addAction(countryAction) 
      else: 
       continentMenu.addAction(countryAction) 

     self.menubar.addAction(self.menuCountry.menuAction()) 
     self.retranslateUi(MainWindow) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) 
     self.menuCountry.setTitle(_translate("MainWindow", "Country", None)) 
     self.menuPopular_Countries.setTitle(_translate("MainWindow", "Popular Countries", None)) 
     self.actionAll_Countries.setText(_translate("MainWindow", "All Countries", 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_()) 

このコードではあまり説明しません。このエラーは、大陸= {}で始まるブロックのどこかにある可能性が高いため、これはcountryActionが定義されている場所です。

答えて

1

あなたがラムダではなく、部分的に使用する必要があります

from functools import partial 
countryAction.triggered.connect(partial(self.countryAction, country)) 
+0

ありがとう、これは素晴らしいです。あなたはそれの背後にある論理を説明できますか?関数がラップされたときにのみ呼び出されるため、最後のcountryActionが常にトリガーされるラムダの効果とは何か? –

0

あなたのコードは私にとってはうまくいくようです。 CentOSの@ jkelly

::〜$回転数-qi PyQt4 バージョン:4.10.1リリース :私はx86_64版上のCentOS-7によ13.el7

+0

変更

countryAction.triggered.connect(lambda: self.countryAction()) 

それが実行されますが、あなたはをクリックするかどうかを 'イギリス' というタイトルのウィンドウを得ることはありませんオランダ、イギリス? –

+1

そうです。私がラムダを返す関数を使用すると、それは動作します。

 def cfunc(self, country): return lambda x: self.countryAction(country) ... countryAction.triggered.connect(self.cfunc(country)) 

+0

また、国をself.countryActionの引数として追加する必要がありました –

1

あなたは私たちが機能text()を使用する名前を取得するには、この場合QActionには、信号を生成したオブジェクトを返すsender()機能を使用することができ、彼らのために私たちは次のように変更します。

countryAction.triggered.connect(self.countryAction) 

そして


def countryAction(self): 
    window = QtGui.QMainWindow(self) 
    window.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
    window.setWindowTitle(self.tr(self.sender().text())) 
    window.show() 
関連する問題