Posted By

vkolev on 11/03/10


Tagged

google python dictionary pyqt4


Versions (?)

qPyDict


 / Published in: Python
 

URL: https://bitbucket.org/vkolev/gdict/overview

It's absolutly the same as the gDict, that I wrote with pyGTK2, but now with Qt4 GUI just for testing.

  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'qgdict.ui'
  4. #
  5. # Created: Tue Nov 2 14:46:22 2010
  6. # by: PyQt4 UI code generator 4.7.2
  7. #
  8. # WARNING! All changes made in this file will be lost!
  9.  
  10. languages = ['??', 'ar', 'bg', 'cs', 'de', 'en', 'es', 'fr', 'it', 'pt']
  11. url = 'http://ajax.googleapis.com/ajax/services/language/translate'
  12. params = {}
  13. params['v'] = "1.0"
  14. params['q'] = ""
  15. params['langpair'] = ""
  16.  
  17. import codecs
  18. import urllib
  19. import simplejson
  20. from PyQt4 import QtCore, QtGui
  21.  
  22. class Ui_MainWindow(object):
  23. def setupUi(self, MainWindow):
  24. MainWindow.setObjectName("MainWindow")
  25. MainWindow.resize(428, 276)
  26. MainWindow.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
  27. icon = QtGui.QIcon()
  28. icon.addPixmap(QtGui.QPixmap("dictionary.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
  29. MainWindow.setWindowIcon(icon)
  30. MainWindow.setAutoFillBackground(False)
  31. self.aboutdialog = QtGui.QWidget()
  32. self.centralwidget = QtGui.QWidget(MainWindow)
  33. self.centralwidget.setObjectName("centralwidget")
  34. self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
  35. self.verticalLayout_2.setObjectName("verticalLayout_2")
  36. self.gridLayout = QtGui.QGridLayout()
  37. self.gridLayout.setObjectName("gridLayout")
  38. self.horizontalLayout = QtGui.QHBoxLayout()
  39. self.horizontalLayout.setObjectName("horizontalLayout")
  40. self.comboFrom = QtGui.QComboBox(self.centralwidget)
  41. self.comboFrom.setObjectName("comboFrom")
  42. for lang in languages:
  43. self.comboFrom.addItem(lang)
  44. self.horizontalLayout.addWidget(self.comboFrom)
  45. self.gridLayout.addLayout(self.horizontalLayout, 1, 0, 1, 1)
  46. self.comboTo = QtGui.QComboBox(self.centralwidget)
  47. self.comboTo.setObjectName("comboTo")
  48. for lang in languages:
  49. if lang == "??":
  50. continue
  51. self.comboTo.addItem(lang)
  52. self.gridLayout.addWidget(self.comboTo, 2, 0, 1, 1)
  53. self.aboutButton = QtGui.QPushButton(self.centralwidget)
  54. icon1 = QtGui.QIcon()
  55. icon1.addPixmap(QtGui.QPixmap("info.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
  56. self.aboutButton.setIcon(icon1)
  57. self.aboutButton.setObjectName("aboutButton")
  58. self.gridLayout.addWidget(self.aboutButton, 3, 0, 1, 1)
  59. self.pasteButton = QtGui.QPushButton(self.centralwidget)
  60. icon2 = QtGui.QIcon()
  61. icon2.addPixmap(QtGui.QPixmap("clipboard.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
  62. self.pasteButton.setIcon(icon2)
  63. self.pasteButton.setObjectName("pasteButton")
  64. self.gridLayout.addWidget(self.pasteButton, 3, 1, 1, 1)
  65. self.translateButton = QtGui.QPushButton(self.centralwidget)
  66. icon3 = QtGui.QIcon()
  67. icon3.addPixmap(QtGui.QPixmap("bubble.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
  68. self.translateButton.setIcon(icon3)
  69. self.translateButton.setObjectName("translateButton")
  70. self.gridLayout.addWidget(self.translateButton, 3, 2, 1, 1)
  71. self.label = QtGui.QLabel(self.centralwidget)
  72. sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
  73. sizePolicy.setHorizontalStretch(0)
  74. sizePolicy.setVerticalStretch(0)
  75. sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
  76. self.label.setSizePolicy(sizePolicy)
  77. self.label.setMinimumSize(QtCore.QSize(0, 144))
  78. self.label.setText("None")
  79. self.label.setPixmap(QtGui.QPixmap("dictionary.png"))
  80. self.label.setScaledContents(False)
  81. self.label.setAlignment(QtCore.Qt.AlignCenter)
  82. self.label.setObjectName("label")
  83. self.gridLayout.addWidget(self.label, 0, 0, 1, 3)
  84. self.textEdit = QtGui.QLineEdit(self.centralwidget)
  85. sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
  86. sizePolicy.setHorizontalStretch(0)
  87. sizePolicy.setVerticalStretch(0)
  88. sizePolicy.setHeightForWidth(self.textEdit.sizePolicy().hasHeightForWidth())
  89. self.textEdit.setSizePolicy(sizePolicy)
  90. self.textEdit.setMinimumSize(QtCore.QSize(0, 40))
  91. self.textEdit.setObjectName("textEdit")
  92. self.gridLayout.addWidget(self.textEdit, 1, 1, 1, 2)
  93. self.plainTextEdit = QtGui.QPlainTextEdit(self.centralwidget)
  94. sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
  95. sizePolicy.setHorizontalStretch(0)
  96. sizePolicy.setVerticalStretch(0)
  97. sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth())
  98. self.plainTextEdit.setSizePolicy(sizePolicy)
  99. self.plainTextEdit.setMinimumSize(QtCore.QSize(0, 50))
  100. self.plainTextEdit.setBaseSize(QtCore.QSize(0, 50))
  101. self.plainTextEdit.setObjectName("plainTextEdit")
  102. self.gridLayout.addWidget(self.plainTextEdit, 2, 1, 1, 2)
  103. self.verticalLayout_2.addLayout(self.gridLayout)
  104. MainWindow.setCentralWidget(self.centralwidget)
  105.  
  106. self.translateButton.connect(self.translateButton, QtCore.SIGNAL('clicked()'), self.OnTranslate)
  107. self.aboutButton.connect(self.aboutButton, QtCore.SIGNAL('clicked()'), self.OnShowAbout)
  108. self.pasteButton.connect(self.pasteButton, QtCore.SIGNAL('clicked()'), self.OnPasteClipboard)
  109.  
  110. self.retranslateUi(MainWindow)
  111. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  112.  
  113. def retranslateUi(self, MainWindow):
  114. MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "qGDict", None, QtGui.QApplication.UnicodeUTF8))
  115. self.aboutButton.setText(QtGui.QApplication.translate("MainWindow", "About", None, QtGui.QApplication.UnicodeUTF8))
  116. self.pasteButton.setText(QtGui.QApplication.translate("MainWindow", "Paste", None, QtGui.QApplication.UnicodeUTF8))
  117. self.translateButton.setText(QtGui.QApplication.translate("MainWindow", "Translate", None, QtGui.QApplication.UnicodeUTF8))
  118.  
  119. def OnTranslate(self):
  120. #self.plainTextEdit.appendPlainText("The translated text comes here!: %s" % self.textEdit.displayText())
  121. translated = self.TranslateString()
  122. self.plainTextEdit.clear()
  123. self.plainTextEdit.appendPlainText("%s" % translated)
  124.  
  125. def OnShowAbout(self):
  126. QtGui.QMessageBox.information(self.aboutdialog, "About qPydict", "A PyQt4 application for translating\ndifferent texts in various languages\nusing the google trnsation APIs\n\nAuthor: Vladimir Kolev\nLicense: GNU/GPL v3.")
  127.  
  128. def OnPasteClipboard(self):
  129. self.textEdit.paste()
  130.  
  131. def TranslateString(self):
  132. langfrom = self.comboFrom.currentText()
  133. if langfrom == "??":
  134. langfrom = ""
  135. langto = self.comboTo.currentText()
  136. params['langpair'] = "%s|%s" % (langfrom, langto)
  137. params['q'] = self.textEdit.displayText().toUtf8()
  138. data = urllib.urlencode(params)
  139. response = urllib.urlopen(url, data)
  140. json = simplejson.load(response)
  141. return json['responseData']['translatedText']
  142.  
  143.  
  144. if __name__ == "__main__":
  145. import sys
  146. app = QtGui.QApplication(sys.argv)
  147. MainWindow = QtGui.QMainWindow()
  148. ui = Ui_MainWindow()
  149. ui.setupUi(MainWindow)
  150. MainWindow.show()
  151. sys.exit(app.exec_())

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: vkolev on November 3, 2010

Here a screenshot: screenshot

You need to login to post a comment.