This is the code that should use a simple form with a button and a label.

#-*- coding: utf-8 -*- from PyQt4 import uic, QtCore, QtGui, uic import sys app = QtGui.QApplication(sys.argv) window = uic.loadUi("MyForm.ui") QtCore.QObject.connect(window.btnQuit, QtCore.SIGNAL("clicked()"), QtGui.qApp.quit) window.show() sys.exit(app.exec_()) 

I get an error:

 Traceback (most recent call last): File "G:\Python32\освоение pyqt\start2.py", line 5, in <module> window = uic.loadUi("MyForm.ui") File "G:\Python32\lib\site-packages\PyQt4\uic\__init__.py", line 219, in loadUi from PyQt4.uic.Loader.loader import DynamicUILoader UnicodeEncodeError: 'latin-1' codec can't encode characters in position 12-19: ordinal not in range(256) 

I don’t know what the problem is ... The form itself (.ui file) is saved in utf-8, the script itself also works in utf-8 ... Where to fix / convert where - I won’t put my mind to it.

  • one
    And why import uic from PyQt4 twice? Immediately I remembered spam, sausages, bacon, spam :) - And
  • Jamb copy-paste from listing, no more :) but thanks for the comment. - Izengardjke

2 answers 2

File "G: Python32 mastering pyqtstart2.py"

Apparently there is a problem, you can not recode Cyrillic to latin-1

Just characters from 12 to 19, as in and written in the error message.

Change the path, remove Cyrillic characters, name it like "learning python" instead of "mastering python".

  • Everything turned out to be so simple that I didn’t even see it. :( - Izengardjke
  • Python knows how to surprise, if you do not know how to understand what it says. ) - Alexander Molofeev

And my advice to you is learning to immediately apply the OOP model. Example.

 #-*- coding: utf-8 -*- from PyQt4 import uic, QtCore, QtGui import sys class window(QtGui.QMainWindow): def __init__(self): super(window,self).__init__() uic.loadUi("MyForm.ui",self) self.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), QtGui.qApp.quit) app = QtGui.QApplication(sys.argv) wnd = window() wnd.show() sys.exit(app.exec_()) 
  • Can you briefly explain the conceptual difference? I came across this in the textbook, but I did not understand the special need. - Izengardjke
  • It will take a long time to explain the difference and positive moments, just get accustomed to OPP at once, because sooner or later you yourself will come to this conclusion (in your own way and not only experience). And if in a very short form, the oop gives you a lot of code flexibility. - Alexander Molofeev