Форум сайта python.su
Здраввствуйте уважаемые …
Начинаю изучение Python решил начать с PyQT тк вроде самое быстрое создание GUI для приложений
Имеем:
OS WinXP
Python 2.5.4
PyQt GPL v4.4.3 for Python v2.5
Wing IDE 3.1
Notepad++ :)
Создал с помощью QT Designer меню, сохранил как *.ui
перевёл в *.py формат с помощью команды
pyuic4 -o mynew.py mynew.ui
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'new.ui'
#
# Created: Fri Jun 05 16:28:35 2009
# by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(403, 355)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(240, 320, 161, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.textEdit = QtGui.QTextEdit(Dialog)
self.textEdit.setGeometry(QtCore.QRect(0, 0, 401, 291))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(10, 320, 221, 31))
self.pushButton.setObjectName("pushButton")
self.progressBar = QtGui.QProgressBar(Dialog)
self.progressBar.setGeometry(QtCore.QRect(0, 290, 391, 23))
self.progressBar.setProperty("value", QtCore.QVariant(24))
self.progressBar.setObjectName("progressBar")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Start", None, QtGui.QApplication.UnicodeUTF8))
Отредактировано (Июнь 5, 2009 15:48:35)
Офлайн
Посмотрите здесь, доступно написано, с примерами.
Офлайн
o_nix
Создаете файлик mymain.py следующего содержания:
#!/usr/bin/env python
# coding: utf-8
import sys
import mynew
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyClass(QDialog,
mynew.Ui_Dialog):
def __init__(self, parent=None):
QDialog.__init__(self,parent)
self.setupUi(self)
app = QApplication(sys.argv)
w = MyClass()
w.show()
app.exec_()
Офлайн
gmorgunov, r1s - спасибо помогли … :)
Теперь ещё один вопрос я понимаю что это наверняка самые азы но всётаки почемуто недоходит до меня без примера
Имеем мега приложение cmd без GUI считающее квадрат введённого числа
qad.py
a=input()
s=a*a
print s
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(231, 81)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 10, 91, 61))
self.pushButton.setObjectName("pushButton")
self.lineEdit_3 = QtGui.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(10, 50, 111, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.retranslateUi(Dialog)
# QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.lineEdit.copy)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("Dialog", "поле ввода", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Считать", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit_3.setText(QtGui.QApplication.translate("Dialog", "поле вывода", None, QtGui.QApplication.UnicodeUTF8))
import sys
import menu_qad
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyClass(QDialog,
menu_qad.Ui_Dialog):
def __init__(self, parent=None):
QDialog.__init__(self,parent)
self.setupUi(self)
app = QApplication(sys.argv)
w = MyClass()
w.show()
app.exec_()
Офлайн
В сети есть множество примеров.
файл start_gui.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import menu_qad
import qad
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyClass(QDialog, menu_qad.Ui_Dialog):
def __init__(self, parent=None):
QDialog.__init__(self,parent)
self.setupUi(self)
self.connect(self.pushButton, SIGNAL("clicked()"), self.actionOnButton)
def actionOnButton(self):
"""
self.lineEdit.text() получаем текст
int() нам нужно число
qad.multiplay() вызываем функцию из ммодуля
str() нужно передать строку
self.lineEdit_3.setText() пишем результат
"""
self.lineEdit_3.setText(str(qad.multiplay(int(self.lineEdit.text()))))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyClass()
w.show()
app.exec_()
# -*- coding: UTF-8 -*-
def multiplay(p):
return p*p
Отредактировано (Июнь 8, 2009 18:45:26)
Офлайн
r1s - огромное спасибо, .. всё оказалось запутанее чем я предпологал … буду разбираться и грызть науку :)
Отдельное спасибо за комментарии в коде…
Офлайн
А не будет ли лучше разделить визуальное представление и логику программы?
Офлайн