PYQT:如果选择了组合框中的项目,则设置lineedit的文本



我的问题:如果选择了AD用户,我如何用"employeeID"填充lineedit中的文本

我正在做的事情:我从python运行一个PS脚本,它为我提供了AD用户,然后我从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('^dddddd')
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用户,我需要用他的"employeeID"的AD Users属性在lineedit中设置文本。

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:UsersUSERDesktopusers.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

最新更新