我要在python和pyqt4中设计一个闹钟。
有些问题让我很困惑,首先,我想知道如何为每个textEdit设置固定长度以设置时间(或使用其他方法设置)。 其次,由于我写的代码,当我点击按钮时,textEdit只会显示" ONE"价值;也就是说,如果你点击按钮"一个",textEdit show" 1&#34 ;;但是,如果你点击按钮"两个",textEdit将显示" 2"不是" 12"。我不知道为什么.......
这是我的代码。我将非常感谢您提供的任何帮助。
from PyQt4 import QtCore, QtGui
import sys
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 setalarmclock(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(495, 397)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
Form.setSizePolicy(sizePolicy)
Form.setAutoFillBackground(True)
self.widget = QtGui.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 10, 481, 371))
self.widget.setObjectName(_fromUtf8("widget"))
self.tabWidget = QtGui.QTabWidget(self.widget)
self.tabWidget.setGeometry(QtCore.QRect(0, 10, 471, 341))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.tabWidget.setFont(font)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.gridLayoutWidget = QtGui.QWidget(self.tab)
self.gridLayoutWidget.setGeometry(QtCore.QRect(40, 90, 391, 208))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.pushButton_2 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.pushButton_5 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.gridLayout.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton_4 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.gridLayout.addWidget(self.pushButton_4, 1, 0, 1, 1)
self.pushButton_8 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_8.setFont(font)
self.pushButton_8.setObjectName(_fromUtf8("pushButton_8"))
self.gridLayout.addWidget(self.pushButton_8, 2, 1, 1, 1)
self.pushButton_6 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_6.setFont(font)self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
self.gridLayout.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.pushButton = QtGui.QPushButton(self.gridLayoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMinimumSize(QtCore.QSize(0, 0))
self.pushButton.setSizeIncrement(QtCore.QSize(0, 0))
self.pushButton.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setAutoFillBackground(False)
self.pushButton.setCheckable(False)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_7 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_7.setFont(font)
self.pushButton_7.setObjectName(_fromUtf8("pushButton_7"))
self.gridLayout.addWidget(self.pushButton_7, 2, 0, 1, 1)
self.pushButton_9 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_9.setFont(font)
self.pushButton_9.setObjectName(_fromUtf8("pushButton_9"))
self.gridLayout.addWidget(self.pushButton_9, 2, 2, 1, 1)
self.pushButton_3 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.gridLayout.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.pushButton_10 = QtGui.QPushButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_10.setFont(font)
self.pushButton_10.setObjectName(_fromUtf8("pushButton_10"))
self.gridLayout.addWidget(self.pushButton_10, 3, 1, 1, 1)
self.textEdit = QtGui.QTextEdit(self.tab)
self.textEdit.setGeometry(QtCore.QRect(170, 30, 101, 51))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.textEdit_2 = QtGui.QTextEdit(self.tab)
self.textEdit_2.setGeometry(QtCore.QRect(320, 30, 104, 51))
self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
self.label = QtGui.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(40, 30, 111, 51))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(280, 30, 31, 51))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.gridLayoutWidget_2 = QtGui.QWidget(self.tab_2)
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(10, 180, 441, 111))
self.gridLayoutWidget_2.setObjectName(_fromUtf8("gridLayoutWidget_2"))
self.gridLayout_2 = QtGui.QGridLayout(self.gridLayoutWidget_2)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.pushButton_11 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_11.setFont(font)
self.pushButton_11.setObjectName(_fromUtf8("pushButton_11"))
self.gridLayout_2.addWidget(self.pushButton_11, 0, 0, 1, 1)
self.pushButton_14 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_14.setFont(font)
self.pushButton_14.setObjectName(_fromUtf8("pushButton_14"))
self.gridLayout_2.addWidget(self.pushButton_14, 0, 3, 1, 1)
self.pushButton_13 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_13.setFont(font)
self.pushButton_13.setObjectName(_fromUtf8("pushButton_13"))
self.gridLayout_2.addWidget(self.pushButton_13, 0, 2, 1, 1)
self.pushButton_12 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_12.setFont(font)
self.pushButton_12.setObjectName(_fromUtf8("pushButton_12"))
self.gridLayout_2.addWidget(self.pushButton_12, 0, 1, 1, 1)
self.pushButton_15 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_15.setFont(font)
self.pushButton_15.setObjectName(_fromUtf8("pushButton_15"))
self.gridLayout_2.addWidget(self.pushButton_15, 1, 0, 1, 1)
self.pushButton_16 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_16.setFont(font)
self.pushButton_16.setObjectName(_fromUtf8("pushButton_16"))
self.gridLayout_2.addWidget(self.pushButton_16, 1, 1, 1, 1)
self.pushButton_17 = QtGui.QPushButton(self.gridLayoutWidget_2)
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_17.setFont(font)
self.pushButton_17.setObjectName(_fromUtf8("pushButton_17"))
self.gridLayout_2.addWidget(self.pushButton_17, 1, 2, 1, 1)
self.label_3 = QtGui.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(10, 60, 51, 51))
font = QtGui.QFont()
font.setPointSize(20)
self.label_3.setFont(font)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.textEdit_3 = QtGui.QTextEdit(self.tab_2)
self.textEdit_3.setGeometry(QtCore.QRect(80, 60, 361, 51))
self.textEdit_3.setObjectName(_fromUtf8("textEdit_3"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.tab_3 = QtGui.QWidget()
self.tab_3.setObjectName(_fromUtf8("tab_3"))
self.radioButton = QtGui.QRadioButton(self.tab_3)
self.radioButton.setGeometry(QtCore.QRect(30, 25, 83, 41))
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(self.tab_3)
self.radioButton_2.setGeometry(QtCore.QRect(30, 80, 83, 41))
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.radioButton_3 = QtGui.QRadioButton(self.tab_3)
self.radioButton_3.setGeometry(QtCore.QRect(30, 140, 83, 41))
self.radioButton_3.setObjectName(_fromUtf8("radioButton_3"))
self.textEdit_4 = QtGui.QTextEdit(self.tab_3)
self.textEdit_4.setGeometry(QtCore.QRect(120, 140, 321, 41))
self.textEdit_4.setObjectName(_fromUtf8("textEdit_4"))
self.pushButton_18 = QtGui.QPushButton(self.tab_3)
self.pushButton_18.setGeometry(QtCore.QRect(350, 250, 105, 45))
font = QtGui.QFont()
font.setPointSize(22)
self.pushButton_18.setFont(font)
self.pushButton_18.setObjectName(_fromUtf8("pushButton_18"))
self.tabWidget.addTab(self.tab_3, _fromUtf8(""))
self.retranslateUi(Form)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Alarm Clock", None))
self.pushButton.setText(_translate("Form", "1", None))
self.pushButton_2.setText(_translate("Form", "2", None))
self.pushButton_3.setText(_translate("Form", "3", None))
self.pushButton_4.setText(_translate("Form", "4", None))
self.pushButton_5.setText(_translate("Form", "5", None))
self.pushButton_6.setText(_translate("Form", "6", None))
self.pushButton_7.setText(_translate("Form", "7", None))
self.pushButton_8.setText(_translate("Form", "8", None))
self.pushButton_9.setText(_translate("Form", "9", None))
self.pushButton_10.setText(_translate("Form", "0", None))
self.label.setText(_translate("Form", "TIME SET", None))
self.label_2.setText(_translate("Form", ":", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Form", "TIME", None))
self.pushButton_11.setText(_translate("Form", "MON.", None))
self.pushButton_12.setText(_translate("Form", "TUE.", None))
self.pushButton_13.setText(_translate("Form", "WED.", None))
self.pushButton_14.setText(_translate("Form", "THU.", None))
self.pushButton_15.setText(_translate("Form", "FRI.", None))
self.pushButton_16.setText(_translate("Form", "SAT.", None))
self.pushButton_17.setText(_translate("Form", "SUN.", None))
self.label_3.setText(_translate("Form", "週期", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Form", "PERIOD", None))
self.radioButton.setText(_translate("Form", "1", None))
self.radioButton_2.setText(_translate("Form", "2", None))
self.radioButton_3.setText(_translate("Form", "3", None))
self.pushButton_18.setText(_translate("Form", "FINISH", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("Form", "RINGSTONE", None))
self.pushButton.clicked.connect(self.set1)
self.pushButton_2.clicked.connect(self.set2)
self.pushButton_3.clicked.connect(self.set3)
self.pushButton_4.clicked.connect(self.set4)
self.pushButton_5.clicked.connect(self.set5)
self.pushButton_6.clicked.connect(self.set6)
self.pushButton_7.clicked.connect(self.set7)
self.pushButton_8.clicked.connect(self.set8)
self.pushButton_9.clicked.connect(self.set9)
self.pushButton_10.clicked.connect(self.set0)
self.radioButton_3.clicked.connect(self.filebroswer)
def set1(self,Form):
print
def set2(self,Form):
self.textEdit.setText("2")
def set3(self,Form):
self.textEdit.setText("3")
def set4(self,Form):
self.textEdit.setText("4")
def set5(self,Form):
self.textEdit.setText("5")
def set6(self,Form):
self.textEdit.setText("6")
def set7(self,Form):
self.textEdit.setText("7")
def set8(self,Form):
self.textEdit.setText("8")
def set9(self,Form):
self.textEdit.setText("9")
def set0(self,Form):
self.textEdit.setText("0")
def filebroswer(self,Form):
filter = "mp3(*.mp3)"
filename = QtGui.QFileDialog.getOpenFileNameAndFilter(self,"Open file", "C:\\Users\\MIS\\Desktop", filter)
self.textEdit_4.setText(filename[0])
if __name__ == 'main':
app = QtGui.QApplication(sys.argv)
ex = setalarmclock()
ex.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
self.textEdit.setText()
应该清除文字。但如果它不起作用,请尝试在其前添加self.textEdit.clear()
。
如果要附加文字,请使用self.textEdit.append()
如果您想要文字修改的固定尺寸,请添加您喜欢的尺寸的self.textEdit.setFixedSize()
。
答案 1 :(得分:0)
setText
的{{1}}功能正确地完成了它的工作:它设置了文本。
如果您致电QTextEdit
,则textEdit的文字变为" 2"以前的文字被删除了。
你想要的是textEdit.setText("2")
。它默认写入新行,但有some workarounds。如果您不需要多行,则可以使用QTextEdit.append()
。