我尝试使用Python的QML GUI制作一个简单的计算器。当我给出一些像(a = 4,b = 6,c = a + b)和点击按钮的值时,它可以给出一个结果。但是我无法在python中管理TextInput框。当我给输入框赋值时,如何在Python中使用文本输入框? 感谢
QML GUI:
http://postimg.org/image/o9wt326g5/
QML代码:
import QtQuick 1.1
Rectangle {
id:r
anchors.centerIn: parent
width: 200
height: 200
signal messageRequired;
function updateMessage(text) {
messageText.text = text
}
Column{
width: r.width*0.8
height: r.height*0.8
spacing: 10
anchors.centerIn: parent
Row{
id:ro
spacing:10
Text {
anchors.centerIn: ro.parent
font.bold: true
text: "a"
}
TextInput {
id: a
width: r.width*3/4
height: 20
selectionColor: "#2f8bc5"
fillColor: "lightgray"
font.bold: true
}
}
Row{
spacing:10
Text {
text: "b"
font.bold: true
}
TextInput {
id: b
width: r.width*3/4
height: 20
fillColor: "lightgray"
font.bold: true
}
}
Rectangle {
id: calculate
width: r.width
height: 30
color: "#8a0800"
Text{
anchors.centerIn: calculate
font.bold: true
text:"calculate";color:"white"
}
MouseArea {
anchors.fill: parent
onClicked:messageRequired()
}
gradient: Gradient {
GradientStop {position: 0; color: "#8a0800"}
GradientStop {position: 1; color: "#330009"}
}
}
Row{
spacing:10
Text {
text: "c"
font.bold: true
}
TextInput {
id: messageText
font.bold: true
width: r.width*3/4
height: 20
fillColor: "lightgray"
selectionColor: "#2f8bc5"
font.pixelSize: 12
}}}}
和Python代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView
# Create Qt application and the QDeclarative view
app = QApplication(sys.argv)
#------------------------------------
a=6
b=4
c=a+b
#------------------------------------------
class Now(QObject):
now = Signal(str)
def emit_now(self):
formatted_date = str(c)
self.now.emit(formatted_date)
now = Now()
#------------------------------------
qt_view = QDeclarativeView()
qt_view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
# Create an URL to the QML file
url = QUrl('view.qml')
# Set the QML file and show
qt_view.setSource(url)
qt_view.show()
rootObject = qt_view.rootObject()
rootObject.messageRequired.connect(now.emit_now)
now.now.connect(rootObject.updateMessage)
# Enter Qt main loop
sys.exit(app.exec_())