PYQT: Set text of lineedit if an item in combobox is selected
up vote
0
down vote
favorite
My Question:
How can I fill the text in a lineedit with the 'employeeID' if the ADuser is selected?
What I am doing: I run a PS script from python, which gets me the ADusers, then I take the Output from the PS-Script (the AD-Users basically) and fill it into a Combobox (around 500 'items').
Python code(Edited):
# 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_())
So what I need to do next is: If the AD-User is selected in the Combobox, I need to set the text in a lineedit with the AD-Users attribute of his 'employeeID'.
Powershell code below:
$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 file (I cant show you the names of our employees thats why I edited it a bit):
name employeeID
---- ----------
forename surname 110001
forename surname 110002
forename surname 110003
forename surname 110004
forename surname 110005
forename surname 110006
I appreciate any help how this might work !
python powershell pyqt5
|
show 1 more comment
up vote
0
down vote
favorite
My Question:
How can I fill the text in a lineedit with the 'employeeID' if the ADuser is selected?
What I am doing: I run a PS script from python, which gets me the ADusers, then I take the Output from the PS-Script (the AD-Users basically) and fill it into a Combobox (around 500 'items').
Python code(Edited):
# 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_())
So what I need to do next is: If the AD-User is selected in the Combobox, I need to set the text in a lineedit with the AD-Users attribute of his 'employeeID'.
Powershell code below:
$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 file (I cant show you the names of our employees thats why I edited it a bit):
name employeeID
---- ----------
forename surname 110001
forename surname 110002
forename surname 110003
forename surname 110004
forename surname 110005
forename surname 110006
I appreciate any help how this might work !
python powershell pyqt5
Would you mind showing us a few lines of theusers.txt
file?
– Guimoute
Nov 19 at 11:28
To retrieve the text of a combo box you can doself.cbox.currentText()
and to put something in a line editself.lineedit.setText()
.
– Guimoute
Nov 19 at 11:33
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
1
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My Question:
How can I fill the text in a lineedit with the 'employeeID' if the ADuser is selected?
What I am doing: I run a PS script from python, which gets me the ADusers, then I take the Output from the PS-Script (the AD-Users basically) and fill it into a Combobox (around 500 'items').
Python code(Edited):
# 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_())
So what I need to do next is: If the AD-User is selected in the Combobox, I need to set the text in a lineedit with the AD-Users attribute of his 'employeeID'.
Powershell code below:
$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 file (I cant show you the names of our employees thats why I edited it a bit):
name employeeID
---- ----------
forename surname 110001
forename surname 110002
forename surname 110003
forename surname 110004
forename surname 110005
forename surname 110006
I appreciate any help how this might work !
python powershell pyqt5
My Question:
How can I fill the text in a lineedit with the 'employeeID' if the ADuser is selected?
What I am doing: I run a PS script from python, which gets me the ADusers, then I take the Output from the PS-Script (the AD-Users basically) and fill it into a Combobox (around 500 'items').
Python code(Edited):
# 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_())
So what I need to do next is: If the AD-User is selected in the Combobox, I need to set the text in a lineedit with the AD-Users attribute of his 'employeeID'.
Powershell code below:
$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 file (I cant show you the names of our employees thats why I edited it a bit):
name employeeID
---- ----------
forename surname 110001
forename surname 110002
forename surname 110003
forename surname 110004
forename surname 110005
forename surname 110006
I appreciate any help how this might work !
python powershell pyqt5
python powershell pyqt5
edited Nov 26 at 12:52
asked Nov 19 at 11:10
Filip Stanisic
125
125
Would you mind showing us a few lines of theusers.txt
file?
– Guimoute
Nov 19 at 11:28
To retrieve the text of a combo box you can doself.cbox.currentText()
and to put something in a line editself.lineedit.setText()
.
– Guimoute
Nov 19 at 11:33
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
1
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38
|
show 1 more comment
Would you mind showing us a few lines of theusers.txt
file?
– Guimoute
Nov 19 at 11:28
To retrieve the text of a combo box you can doself.cbox.currentText()
and to put something in a line editself.lineedit.setText()
.
– Guimoute
Nov 19 at 11:33
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
1
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38
Would you mind showing us a few lines of the
users.txt
file?– Guimoute
Nov 19 at 11:28
Would you mind showing us a few lines of the
users.txt
file?– Guimoute
Nov 19 at 11:28
To retrieve the text of a combo box you can do
self.cbox.currentText()
and to put something in a line edit self.lineedit.setText()
.– Guimoute
Nov 19 at 11:33
To retrieve the text of a combo box you can do
self.cbox.currentText()
and to put something in a line edit self.lineedit.setText()
.– Guimoute
Nov 19 at 11:33
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
1
1
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Now that you store the employeeID
in the file, it is easier to solve.
What I modified is shown in comments.
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_())
Two things to note:
Duplicates are not supported. If you have the same name several times but with different employeeID, the line edit will take the value of the first one encountered (or the last one if you remove the
break
). But if there are duplicates that's an issue with your file anyway.Maybe you want to keep the headers we can skip the two first lines using this:
.
for n, line in enumerate(content):
if n<2:
continue
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets aroundtokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.
– Guimoute
Nov 19 at 16:48
Also we could useline.split()
to support any number of spacing between forename, surname and id.
– Guimoute
Nov 19 at 16:50
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
|
show 22 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Now that you store the employeeID
in the file, it is easier to solve.
What I modified is shown in comments.
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_())
Two things to note:
Duplicates are not supported. If you have the same name several times but with different employeeID, the line edit will take the value of the first one encountered (or the last one if you remove the
break
). But if there are duplicates that's an issue with your file anyway.Maybe you want to keep the headers we can skip the two first lines using this:
.
for n, line in enumerate(content):
if n<2:
continue
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets aroundtokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.
– Guimoute
Nov 19 at 16:48
Also we could useline.split()
to support any number of spacing between forename, surname and id.
– Guimoute
Nov 19 at 16:50
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
|
show 22 more comments
up vote
0
down vote
accepted
Now that you store the employeeID
in the file, it is easier to solve.
What I modified is shown in comments.
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_())
Two things to note:
Duplicates are not supported. If you have the same name several times but with different employeeID, the line edit will take the value of the first one encountered (or the last one if you remove the
break
). But if there are duplicates that's an issue with your file anyway.Maybe you want to keep the headers we can skip the two first lines using this:
.
for n, line in enumerate(content):
if n<2:
continue
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets aroundtokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.
– Guimoute
Nov 19 at 16:48
Also we could useline.split()
to support any number of spacing between forename, surname and id.
– Guimoute
Nov 19 at 16:50
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
|
show 22 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Now that you store the employeeID
in the file, it is easier to solve.
What I modified is shown in comments.
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_())
Two things to note:
Duplicates are not supported. If you have the same name several times but with different employeeID, the line edit will take the value of the first one encountered (or the last one if you remove the
break
). But if there are duplicates that's an issue with your file anyway.Maybe you want to keep the headers we can skip the two first lines using this:
.
for n, line in enumerate(content):
if n<2:
continue
Now that you store the employeeID
in the file, it is easier to solve.
What I modified is shown in comments.
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_())
Two things to note:
Duplicates are not supported. If you have the same name several times but with different employeeID, the line edit will take the value of the first one encountered (or the last one if you remove the
break
). But if there are duplicates that's an issue with your file anyway.Maybe you want to keep the headers we can skip the two first lines using this:
.
for n, line in enumerate(content):
if n<2:
continue
edited Nov 26 at 11:35
answered Nov 19 at 14:51
Guimoute
4259
4259
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets aroundtokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.
– Guimoute
Nov 19 at 16:48
Also we could useline.split()
to support any number of spacing between forename, surname and id.
– Guimoute
Nov 19 at 16:50
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
|
show 22 more comments
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets aroundtokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.
– Guimoute
Nov 19 at 16:48
Also we could useline.split()
to support any number of spacing between forename, surname and id.
– Guimoute
Nov 19 at 16:50
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
This looks like a nice approach, ive already tried to implement but i am getting this Errormessage: tostring = tokens[0] + " " + tokens[1] # <--- this is the full name TypeError: can only concatenate list (not "str") to list
– Filip Stanisic
Nov 19 at 15:15
I just noticed the brackets around
tokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.– Guimoute
Nov 19 at 16:48
I just noticed the brackets around
tokens = [line.split('n')]
. Split actually returns a tuple already so we can remove them.– Guimoute
Nov 19 at 16:48
Also we could use
line.split()
to support any number of spacing between forename, surname and id.– Guimoute
Nov 19 at 16:50
Also we could use
line.split()
to support any number of spacing between forename, surname and id.– Guimoute
Nov 19 at 16:50
1
1
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
NICE!!! doesnt throw the error anymore, so what I need to do next is connect the Combobox when the user is choosen 'Forename Surname' and set the 'Lineedit' with the correct 'employeeID' -> the thing that I need actually... You've been a great help Guimoute, thank you very much for taking your time and helping me!!!
– Filip Stanisic
Nov 26 at 9:05
1
1
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
Yeah thats kinda what I thought, Ive tried to solve it with user0402680 code, but I just couldnt do it... You've been a great help in writing my first real python code!! Thanks again!
– Filip Stanisic
Nov 26 at 13:02
|
show 22 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373363%2fpyqt-set-text-of-lineedit-if-an-item-in-combobox-is-selected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Would you mind showing us a few lines of the
users.txt
file?– Guimoute
Nov 19 at 11:28
To retrieve the text of a combo box you can do
self.cbox.currentText()
and to put something in a line editself.lineedit.setText()
.– Guimoute
Nov 19 at 11:33
@Guimoute I edited the txt file and I know that already, the thing is I need to match the text entered into the lineedit with the AD-Users attribute "Employee ID"
– Filip Stanisic
Nov 19 at 12:43
@Guimoute i need to match, for example: combotext.currentext('Max Mustermann'), with his employeeID Attribute, so basically if the currenttext is 'Max Mustermann', I want to fill a Lineedit with his 'employeeID' for example: 1111 but i need to take it from the AD so that it is correct and thats where im stuck at
– Filip Stanisic
Nov 19 at 13:27
1
@Guimoute ive changed the output to how i get it from the ps script to the file
– Filip Stanisic
Nov 19 at 14:38