日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

使用PyQT编写界面程序

發布時間:2023/12/31 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用PyQT编写界面程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用PyQT比QT好在,可以隨時監測函數正確性,省去編譯時間 ! 這是個不小的節省.

1. PyQt: 打開對話框

msgbox = QtGui.QMessageBox(self)# 我的語句是 msgbox = QtGui.QMessageBox(self.Centerwidget)msgbox.setWindowTitle('warning')msgbox.setText("hello")msgbox.show()

2.? 詳細教程,一定要參考網頁:http://www.cppblog.com/mirguest/archive/2012/02/12/165386.html

比如:輸入客戶端顯示:

QtGui.QInputDialog 提供了一個簡單方便的對話框,用于獲取用戶輸入的一個值。輸入值可以是字符串,數字,或者一個列表中的一項。

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QWidget):def __init__(self):super(Example, self).__init__()self.initUI()def initUI(self):self.btn = QtGui.QPushButton('Dialog', self)self.btn.move(20, 20)self.btn.clicked.connect(self.showDialog)self.le = QtGui.QLineEdit(self)self.le.move(130, 22)self.setGeometry(300, 300, 290, 150)self.setWindowTitle('Input dialog')self.show()def showDialog(self):text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog','Enter your name:')if ok:self.le.setText(str(text)) def main():app = QtGui.QApplication(sys.argv)ex = Example()sys.exit(app.exec_()) if __name__ == '__main__':main()

這個例子中用到了一個按鈕和一個行編輯組件。按鈕會顯示一個輸入對話框用于得到文本。而輸入的文本將在行編輯組件中顯示。

text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog','Enter your name:')

這一行顯示了輸入對話框。第一個字符串是對話框的標題,第二個字符串則是對話框中的消息。對話框將返回輸入的文本和一個布爾值。如果點擊了 ok 按鈕,則布爾值為 true ,否則為 false

if ok:self.le.setText(str(text))

從對話框中接收到的文本就被設置到行編輯組件中。

3. 注意事項:由Pyuic轉化的python代碼和直接由pyqt寫的代碼有多不同,如1中所示

4.QtGui.QFileDialog

QtGui.QFileDialog 是允許用戶選擇文件或目錄的對話框。文件可以用于打開或保存。

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we select a file with a QtGui.QFileDialog and display its contents in a QtGui.QTextEdit. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow):def __init__(self):super(Example, self).__init__()self.initUI()def initUI(self):self.textEdit = QtGui.QTextEdit()self.setCentralWidget(self.textEdit)self.statusBar()openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)openFile.setShortcut('Ctrl+O')openFile.setStatusTip('Open new File')openFile.triggered.connect(self.showDialog)menubar = self.menuBar()fileMenu = menubar.addMenu('&File')fileMenu.addAction(openFile)self.setGeometry(300, 300, 350, 300)self.setWindowTitle('File dialog')self.show()def showDialog(self):fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file','/home')f = open(fname, 'r')with f:data = f.read()self.textEdit.setText(data) def main():app = QtGui.QApplication(sys.argv)ex = Example()sys.exit(app.exec_()) if __name__ == '__main__':main()

這個例子中有菜單欄,文本編輯區以及狀態欄。菜單中的選項顯示 QtGui.QFileDialog 用于選擇文件。而文件的內容則載入到文本編輯區。

class Example(QtGui.QMainWindow):def __init__(self):super(Example, self).__init__()

這個例子是基于 QtGui.QMainWindow 組件,因為我們要在中心設置文本編輯區。

fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file','/home')

我們彈出 QtGui.QFileDialog 。在getOpenFileName() 方法中第一個字符串是標題。第二個則是指定對話框的工作目錄。默認情況下,文件過濾為所有文件(* )。

f = open(fname, 'r') with f:data = f.read()self.textEdit.setText(data)

選擇的文件將被讀取,并且其文件內容設置到文本編輯區。


5.一個完整的文本編輯器

參考鏈接:http://blog.csdn.net/kilvdn/article/details/4077183


總結

以上是生活随笔為你收集整理的使用PyQT编写界面程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。