0
How to create app that update automatically?
think we have an app with a window include a qlineedit, a qcombobox and a add button, we want add anything that write in qlineedit to qcombobox with add button, this will happen when I close and run app again. why? can write code to add qlineedit contents to qcombobox simultaneously after click on add button?
3 odpowiedzi
0
this code added qlineedit contents to qcombobox, but when I save combobox items to a json file to load from there, I have problem.
problem: when add something to qcombobox that added to json file but dont show in qcombobox, to show last added item in qcombobox thats need to close and run app again. can help me please??
0
Same as I told you before
from PyQt5.QtWidgets import qApp, QApplication, QPushButton, QComboBox, QLineEdit, QWidget, QVBoxLayout
import json
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.createWindow()
def createWindow(self):
btn = QPushButton('ADD')
btn.clicked.connect(self.on_click)
self.le = QLineEdit()
self.cb = QComboBox()
try:
with open('data.json', 'r') as f:
f = f.read().replace('\"', ' ')
self.cb.addItems(f.split())
except:
open('data.json', 'w')
qvb = QVBoxLayout()
qvb.addWidget(btn)
qvb.addWidget(self.le)
qvb.addWidget(self.cb)
self.setLayout(qvb)
def on_click(self):
le_text = self.le.text()
with open('data.json', 'a') as f:
json.dump(le_text, f)
self.cb.addItem(le_text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
- 1
btn = qpushbutton()
btn.clicked.connect(self.on_click)
self.le = qlineedit()
self.cb = qcombobox()
def on_click(self)
text = self.le.text()
self.cb.additem(text)