以下是我的部分分析GUI的代码。单击菜单中的国家/地区时,应生成与该国家/地区不同的菜单。然而,目前,无论哪个国家被按下,最后一个国家将是生成菜单的人(在这种情况下点击任一国家将产生荷兰的菜单)。这是为什么?
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的位置。
答案 0 :(得分:1)
您可能需要使用partial而不是lambda
from functools import partial
countryAction.triggered.connect(partial(self.countryAction, country))
答案 1 :(得分:0)
您的代码似乎对我有用。我在x86_64上使用CentOS-7:
jkelly @ centos:〜$ rpm -qi PyQt4 版本:4.10.1 发布:13.el7