2014年11月12日 星期三

PyQt教學(1)-認識常用的函數

#引入常用的函數
from PyQt4.QtGui  import *
from PyQt4.QtCore import *

#定時器使用範例(一秒後打印!)
QTimer.singleShot(1000, lambda: print('!'))

#配置主視窗
widget.showMaximized()  #最大螢幕,不隱藏上方工具列
widget.showFullScreen() #最大螢幕,隱藏上方工具列
widget.setFixedSize(1,1)#固定視窗大小

#QTextEdit-文本框常用函數
text.setReadOnly(True)                 #唯讀
text.moveCursor(QTextCursor.Start)     #移動游標
text.moveCursor(QTextCursor.Down)      #游標往下一行
text.setLineWrapMode(QTextEdit.NoWrap) #水平滾軸
text.toPlainText()                     #文本內容
text.setTextColor(QColor('#003DF5'))   #變換文字顏色
text.setAcceptRichText(False)          #禁用HTML
text.verticalScrollBar().setEnabled(0) #禁用滾軸
text.insertPlainText('hello')          #在游標處插入字串
text.setVerticalScrollBarPolicy(
    Qt.ScrollBarAlwaysOff)             #隱藏垂直滾軸

#兩文本框垂直滾軸在滑鼠拉動時同步
textA.verticalScrollBar().valueChanged.connect(
    textB.verticalScrollBar().setValue)

#將兩文本框的垂直滾軸同步
textA.verticalScrollBar().setValue(
    textB.verticalScrollBar().value())

#QTextCursor-游標常用函數
cursor = text.textCursor()      #取得文本框的游標
curS = cursor.selectionStart()  #選取中,開頭字元的位置
curE = cursor.selectionEnd()    #選取中,結束字元的位置
cursor.setPosition(curE)        #設置游標的字元位置

#將游標移動到該行最後面
cursor.movePosition(QTextCursor.EndOfLine)
cursor.columnNumber()           #取得游標所在行的位置(行)
cursor.blockNumber()            #取得游標所在行的位置(列)

#選取該行的文本(表面看不到)
cursor.select(QTextCursor.LineUnderCursor)

cursor.selectedText()           #取得選取的文本內容
cursor.hasSelection()           #判斷是否有內容被選取
cursor.deletePreviousChar()     #刪除游標所在的前一個字元
cursor.insertText()             #在游標處插入字串

#QFileDialog-開檔,存檔
QFileDialog.getOpenFileName(
    widget, 'Open file', 'default name')

QFileDialog.getSaveFileName(
    widget, "Save file", "", ".txt")

QFileDialog.getOpenFileName(
    None, "", "","Text files (*.txt);;XML files (*.xml)" )

#QMessageBox-詢問
reply = QMessageBox.question(
    widget,'Message',"Quit?",
    QMessageBox.Yes,
    QMessageBox.No)

print('yes' if reply == QMessageBox.Yes else 'no')

#obj可能為任何種類的UI元件
obj.setEnabled(False)                        #禁用元件
obj.show()                                   #顯示元件
obj.hide()                                   #隱藏元件
obj.setFocus()                               #設置焦點
obj.setCursor(QCursor(Qt.PointingHandCursor))#更換游標
obj.setFont(QFont("consolas", 20, 80, False))#字型配置
obj.setPixmap(QPixmap('mute.png'))           #載入圖片
obj.setPixmap(QPixmap(''))                   #移除圖片
obj.setText('string')                        #設置字串
obj.width()                                  #元件寬
obj.height()                                 #元件高

#取得螢幕的寬和高
screen = app.desktop().size()
print(screen.height(),screen.width())

#用CSS語法改變樣式(若調用第二次則會覆蓋先前那次!)
obj.setStyleSheet('color: yello')
obj.setStyleSheet('color: red; border: 2px solid blue;')

#QCheckBox(調整框框大小)
widget.setStyleSheet('''
    QCheckBox::indicator{
    width: 40px;
    height: 40px;
}''')

#操控剪貼板
clipboard = QApplication.clipboard()
clipboard.setText('apple')
#此時可在編輯器中用ctrl+v,會貼出'apple'的字樣


                                                                           上一篇    回目錄    下一篇

沒有留言:

張貼留言