2017-04-03 49 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_()) 

在這段代碼中沒有太多解釋。該錯誤可能在以continents = {}開頭的塊中的某處,因爲這是countryAction定義的位置。

回答

1

您可能需要使用的部分,而不是拉姆達

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

謝謝,這很有用。你能解釋它背後的邏輯嗎?這與lambda的效果有什麼關係,只是在被點擊時調用函數,因此總是觸發最後一個countryAction? –

0

您的代碼似乎適用於我。我在CentOS-7上的x86_64的:

jkelly @ CentOS的:〜$轉-qi PyQt4的 版本:4.10.1 發佈:13.el7

+0

它運行,但你沒有得到名爲 '英國' 你是否點擊該窗口荷蘭還是英國? –

+1

你是對的。如果我使用一個函數來返回lambda,它的工作原理。

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

+0

還必須將國家作爲參數添加到self.countryAction –

1

可以使用sender()函數返回生成信號,在這種情況下QAction對象,讓我們用函數text()的名字,爲他們作出如下改變。

變化

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

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()