Python QT5组合框-保存列表,打开方法
我想在一个单独的窗口中添加一个数字,在新应用程序启动后,它将显示在一个组合框中。也许是一些基础,我只是不知道该怎么做。这是我的密码: 也许我可以加一些基础Python QT5组合框-保存列表,打开方法,python,combobox,qt5,Python,Combobox,Qt5,我想在一个单独的窗口中添加一个数字,在新应用程序启动后,它将显示在一个组合框中。也许是一些基础,我只是不知道该怎么做。这是我的密码: 也许我可以加一些基础 from PyQt5.QtWidgets import QLabel,QDialogButtonBox,QTableWidget, QTableWidgetItem, \ QVBoxLayout, QPushButton, QAction, QLineEdit, QMessageBox, QDateEdit,QHBoxLayout,
from PyQt5.QtWidgets import QLabel,QDialogButtonBox,QTableWidget, QTableWidgetItem, \
QVBoxLayout, QPushButton, QAction, QLineEdit, QMessageBox, QDateEdit,QHBoxLayout, QGridLayout
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets, QtGui
class Klient1(QWidget):
def __init__(self):
super(Klient1, self).__init__()
self.setWindowTitle("Klient")
self.setFixedSize(300, 200)
self.initUI()
def initUI(self):
self.combobox = QtWidgets.QComboBox(self)
self.combobox.move(10,10)
self.combobox.resize(100,25)
self.btn = QtWidgets.QPushButton('otworz',self)
self.btn.clicked.connect(self.add)
self.btn.move(10,50)
self.btn.resize(100,25)
def add(self,nowe):
dodaj = Dodaj_zlecenie()
dodaj.exec_()
wartosc = (dodaj.numer)
self.combobox.addItem(wartosc)
dodaj.show()
下一窗口:
class Dodaj_zlecenie(QDialog):
def __init__(self, *args, **kwargs):
super(Dodaj_zlecenie, self).__init__(*args, **kwargs)
self.QBtn = QPushButton()
self.QBtn.setText("Dodaj")
self.setWindowTitle("Dodaj do listy")
self.setFixedWidth(300)
self.setFixedHeight(100)
self.QBtn.clicked.connect(self.nowe)
layout = QVBoxLayout()
self.listinput = QLineEdit()
self.onlyInt = QIntValidator()
self.listinput.setValidator(self.onlyInt)
self.listinput.setPlaceholderText("Numer.")
layout.addWidget(self.listinput)
layout.addWidget(self.QBtn)
self.setLayout(layout)
def nowe(self):
global numer
self.numer = (self.listinput.text())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyle('Fusion')
MW = Klient1()
MW.show()
sys.exit(app.exec_())
我找到了一个目前对我来说足够的解决方案 我将字段中的值条目添加到文本文档中
text_file = open('file1.txt', 'a+')
with open('file1.txt', 'a+') as f:
web_browsers = (wartosc)
f.writelines("%s\n" % line for line in web_browsers)
self.combobox.addItem(web_browsers)
with open("file1.txt", "r") as f:
lines = f.readlines()
a = lines
for i in a:
self.combobox.addItem(i)
然后在“组合框”字段中指定值从文件中删除
text_file = open('file1.txt', 'a+')
with open('file1.txt', 'a+') as f:
web_browsers = (wartosc)
f.writelines("%s\n" % line for line in web_browsers)
self.combobox.addItem(web_browsers)
with open("file1.txt", "r") as f:
lines = f.readlines()
a = lines
for i in a:
self.combobox.addItem(i)
最后,代码如下所示:
from PyQt5.QtWidgets import QLabel,QDialogButtonBox,QTableWidget, QTableWidgetItem, \
QVBoxLayout, QPushButton, QAction, QLineEdit, QMessageBox, QDateEdit,QHBoxLayout, QGridLayout
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets, QtGui
class Klient1(QWidget):
def __init__(self):
super(Klient1, self).__init__()
self.setWindowTitle("Klient")
self.setFixedSize(300, 200)
self.initUI()
def initUI(self):
self.combobox = QtWidgets.QComboBox(self)
with open("file1.txt", "r") as f:
lines = f.readlines()
a = lines
for i in a:
self.combobox.addItem(i)
# self.combobox.addItem(a)
self.combobox.move(10,10)
self.combobox.resize(100,25)
self.btn = QtWidgets.QPushButton('otworz',self)
self.btn.clicked.connect(self.add)
self.btn.move(10,50)
self.btn.resize(100,25)
def add(self,nowe):
global wartosc
dodaj = Dodaj_zlecenie()
dodaj.exec_()
wartosc = (dodaj.numer)
text_file = open('file1.txt', 'a+')
with open('file1.txt', 'a+') as f:
web_browsers = (wartosc)
f.writelines("%s\n" % line for line in web_browsers)
self.combobox.addItem(web_browsers)
dodaj.show()
class Dodaj_zlecenie(QDialog):
def __init__(self, *args, **kwargs):
super(Dodaj_zlecenie, self).__init__(*args, **kwargs)
self.QBtn = QPushButton()
self.QBtn.setText("Dodaj")
self.setWindowTitle("Dodaj do listy")
self.setFixedWidth(300)
self.setFixedHeight(100)
self.QBtn.clicked.connect(self.nowe)
layout = QVBoxLayout()
self.listinput = QLineEdit()
self.onlyInt = QIntValidator()
self.listinput.setValidator(self.onlyInt)
self.listinput.setPlaceholderText("Numer.")
layout.addWidget(self.listinput)
layout.addWidget(self.QBtn)
self.setLayout(layout)
def nowe(self):
global numer
self.numer = (self.listinput.text())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyle('Fusion')
MW = Klient1()
MW.show()
sys.exit(app.exec_())
也许你有更好的解决办法