[英]PYQT: Set text of lineedit if an item in combobox is selected
我的问题: 如果选择了ADuser,如何用'employeeID'填充lineedit中的文本?
我正在做什么:我从python运行了PS脚本,这使我获得了ADuser,然后从PS脚本(基本上是AD用户)获取了输出,并将其填充到了一个组合框(大约500个“项”)中。
Python代码(已编辑):
# NOTE: this is not the full code, just the full code for solving the problem
def __init__(self):
super().__init__()
self.__initUI__()
def __initUI__(self):
self.vorgesetzter()
self.persnum_supervisor()
self.fill_the_combo_box()
self.Vorgesetzte.currentIndexChanged.connect(self.display_employee_id)
def fill_the_combo_box(self):
"""Filling the combo box with the names extracted from a file."""
subprocess.Popen(["powershell.exe", "C:\\Users\\User\\Desktop\\Get-ADUser.ps1"], stdout=subprocess.PIPE, universal_newlines=True, shell=True).communicate()
lines = open('C:\\Users\\User\\Desktop\\users.txt').readlines()
open('C:\\Users\\User\\Desktop\\newusers.txt', 'w').writelines(lines[3:])
with open("C:\\Users\\User\\Desktop\\newusers.txt", 'r', encoding='utf8') as f:
content = f.readlines()
for line in content:
tokens = line.split()
if len(tokens) < 2:
continue # <--- skip the empty line of the file
tostring = tokens[0] + " " + tokens[1] # <--- this is the full name
self.Vorgesetzte.addItems([tostring])
def display_employee_id(self):
"""Checking whether the currently selected combo box name has an employeeID stored in the file."""
with open("C:\\Users\\User\\Desktop\\users.txt", 'r', encoding='utf8') as f:
selected_name = self.Vorgesetzte.currentText()
content = f.readlines()
for line in content:
tokens = line.split()
if len(tokens) < 2:
continue
full_name = tokens[0] + " " + tokens[1]
employeeID = str(tokens[2]) if len(tokens)==3 else "no id found!" # <-- support for absence of employeeID in the file, put whatever string you like here
if selected_name == full_name:
self.persnum_supervisor.setText(employeeID)
break
def vorgesetzter(self):
"""Eingabefeld für den Vorgesetzten"""
self.Vorgesetzte = QComboBox(self)
self.Vorgesetzte.setEditable(True)
self.Vorgesetzte.completer()
font = self.Vorgesetzte.font()
font.setPointSize(9)
self.Vorgesetzte.setFont(font)
self.Vorgesetzte.setFixedSize(250, 20)
self.Vorgesetzte.move(150, 210)
self.VorgesetzteBlock = QLabel(self)
self.VorgesetzteBlock.move(10, 210)
self.VorgesetzteBlock.setText("Vorgesetzte/r:")
def personalnum_supervisor(self):
"""TEXTLINE FÜR PERSONALNUMMER SUPERVISOR"""
self.persnum_supervisor = QLineEdit(self)
self.persnum_supervisor.setMaxLength(20)
font = self.persnum_supervisor.font()
font.setPointSize(9)
self.persnum_supervisor.setFont(font)
regex = QRegularExpression('^\d\d\d\d\d\d')
validsuper_vis = QRegularExpressionValidator(regex)
self.persnum_supervisor.setValidator(validsuper_vis)
self.persnum_supervisor.move(750, 300)
self.persnum_supervisor.setFixedSize(250, 20)
self.persnum_supervisorBlock = QLabel(self)
self.persnum_supervisorBlock.move(500, 300)
self.persnum_supervisorBlock.setText("Personalnummer(Vorgesetzter):")
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
因此,我接下来需要做的是:如果在组合框中选择了AD用户,则需要在lineedit中使用“ employeeID”的AD用户属性设置文本。
下面的Powershell代码:
$s = "OU=,DC=,DC="
$User = Get-ADUser -Filter * -Properties name, employeeID -Searchbase $s |
Select-Object -Property name,employeeID
$User | Sort-Object -CaseSensitive | Out-File -Encoding utf8 C:\Users\USER\Desktop\users.txt
Users.txt文件(我无法向您显示我们员工的姓名,这就是为什么我对其进行了一些编辑):
name employeeID
---- ----------
forename surname 110001
forename surname 110002
forename surname 110003
forename surname 110004
forename surname 110005
forename surname 110006
感谢您提供的帮助!
现在您将employeeID
存储在文件中,因此更易于解决。
我修改的内容显示在注释中。
from PyQt5 import QtWidgets,QtGui,QtCore
import sys
import subprocess
class Widget(QtWidgets.QWidget):
def __init__(self,parent=None):
super(Widget,self).__init__(parent=None)
self.cbox = QtWidgets.QComboBox(self)
self.setGeometry(100,100,300,300)
self.fill_the_combo_box()
self.lineEdit = QtWidgets.QLineEdit(self)
self.lineEdit.setGeometry(100,100,100,100)
self.cbox.currentIndexChanged.connect(self.display_employee_id) # will trigger every time you select a new name in the combo box.
def fill_the_combo_box(self):
"""Filling the combo box with the names extracted from a file."""
subprocess.Popen(["powershell.exe", "C:\\Users\\USER\\Desktop\\Get-ADUser.ps1"], stdout=subprocess.PIPE, universal_newlines=True, shell=True).communicate()
lines = open('C:\\Users\\USER\\Desktop\\users.txt').readlines()
open('C:\\Users\\USER\\Desktop\\newusers.txt', 'w').writelines(lines[3:])
with open("C:\\Users\\USER\\Desktop\\newusers.txt", 'r', encoding='utf8') as f:
content = f.readlines()
for line in content:
tokens = line.split()
if len(tokens) < 2:
continue # <--- skip the empty line of the file
tostring = tokens[0] + " " + tokens[1] # <--- this is the full name
tostringcleared = tostring.replace("[", "").replace("'", "").replace('\\ufeff',"").replace("]", "").replace(",", "") #clear the strings from special characters
self.cbox.addItems([tostringcleared])
def display_employee_id(self):
"""Checking whether the currently selected combo box name has an employeeID stored in the file."""
with open("C:\\Users\\USER\\Desktop\\users.txt", 'r', encoding='utf8') as f:
selected_name = self.cbox.currentText()
content = f.readlines()
for line in content:
tokens = line.split()
full_name = tokens[0] + " " + tokens[1]
employeeID = str(tokens[2]) if len(tokens)==3 else "no id found!" # <-- support for absence of employeeID in the file, put whatever string you like here
if selected_name == full_name:
self.lineedit.setText(employeeID)
break
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
有两件事要注意:
不支持重复。 如果您多次使用相同的姓名,但使用不同的employeeID,则行编辑将采用遇到的第一个(如果删除break
则为最后一个)的值。 但是,如果存在重复,则文件仍然存在问题。
也许您想保留标题,我们可以使用以下方法跳过前两行:
。
for n, line in enumerate(content):
if n<2:
continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.