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 !










share|improve this question
























  • 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










  • @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















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 !










share|improve this question
























  • 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










  • @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













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 !










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • @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










  • 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 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












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





share|improve this answer























  • 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










  • Also we could use line.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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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





share|improve this answer























  • 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










  • Also we could use line.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















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





share|improve this answer























  • 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










  • Also we could use line.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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 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






  • 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










  • 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






  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?