- Python3
- PyQt5 5.15.4
- Qt Designer
- xml.etree.ElementTree
- XML : 利用
xml.etree.ElementTree
來讀取.xml檔案,並取得ArticleTitle(標題)、AbstractText(內文)。 - json: 利用
json
來讀取.json檔,並取得full_name、username、tweet_text(內文)這三個key的value來表示。
- 先用Qt Designer設計好介面並產生一個
.ui
檔 - 於存在
.ui
檔的目錄下輸入指令pyuic5 -x example.ui -o example_ui.py
即可產生一個.py
檔 用 Qt Designer 來設計 PyQt GUI 應用程式界面
btn = QtWidgets.QPushButton('Twitter')
# button click事件
btn.clicked.connect(partial(self.add_JsonTitle, count_List))
item = QtWidgets.QListWidgetItem()
# 將item新增到list
self.titleList.addItem(item)
item.setSizeHint(QtCore.QSize(12, 30)) # 調整item大小
# 將widget新增到item
self.titleList.setItemWidget(item, btn)
因為需要透過click事件傳入文章資料,所以我們可以利用functools
裡頭的partial
函數來處理
button.clicked.connect(partial(self.funtion, 欲傳入參數))
另外還有利用lambda
來傳遞的方法在連結裡面。
在button點選事件中,用button = self.sender()
獲取按鈕物件
參考: QListWidget實現點選按鈕來獲取當前行
此次專題顯示文件使用QTextBrowser,而search框使用QTextEdit的原因是QTextEdit可以做讀寫的動作,而顯示文件因不需要輸入所以使用QTextBrowser。 參考:QTextEdit和QTextBrowser差異
QTextBrowser可用HTML格式顯示
注意:轉成HTML格式一些符號格式也需要更改
原 | 更改 |
---|---|
\n |
<br> |
< |
< |
> |
> |
> 寫入(會重置):`QTextBrowser.setText('輸入')`
> 追加(不會重置): `QTextBrowser.append('輸入')`
> str1= self.lineEdit.text() # 單行顯示
- insert
# str.insert(index, 要插入的句子)
aList = [123, 'xyz', 'zara', 'abc']
aList.insert( 3, 2009)
# output = [123, 'xyz', 'zara', 2009, 'abc']
- 直接加
line = 'Kong Panda'
index = line.find('Panda')
output_line = line[:index] + 'Fu ' + line[index:]
# output = 'Kong Fu Panda'
# 利用空格、句點區分
self.characters.setText('Number of characters: {0} characters.'.format(len(text)))
self.words.setText('Number of words: {0} words.'.format(len(text.split(' '))))
self.sentences.setText('Number of sentences: {0} sentences.'.format(len(text.split('.'))))
使用正規表示式re.finditer
來查找,與 re.findall
相比的額外資訊,比如有關字串中匹配位置的資訊(索引)。Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string.
match.span(): 回傳一個tuple(start(), end()) match.group(): 回傳匹配值
並統一用小寫str(text).lower()
來做搜尋比對。
-
在當前目錄下輸入
python3 main.py
HighLighting potions of text in QPlainTextEdit
python多層list到str的轉換 和 str中換行符\n的保存
How to convert a list of characters into a string in Python
Use
str.join()
Call str.join(iterable) with the empty string "" as str and a list as iterable to join each character into a string.
a_list = ["a", "b", "c"] a_string = "".join(a_list) # output = abc
Python dict.get()方法 http://tw.gitbook.net/python/dictionary_get.html
Python replace()方法 https://www.runoob.com/python/att-string-replace.html
Python split()方法 https://www.runoob.com/python/att-string-split.html
re — Regular expression operations 使用 re.finditer 迭代匹配 深入理解 Python 的 re 模組