Comparing two strings in different widgets
-
Am trying to compare two strings in two widgets ( a text browser and a line edit) using pyqt5 and return "same files" or "different files" if the strings are the same or different, in another widget, but I can't seem to get it right. When I execute my GUI, when I click the compare button using this method, it keeps executing only the "else" part even when the two strings contained in the two widgets are exactly the same. Below is my code snippet, any help will be welcome
def hashComp(self):
v1 = self.textBrowser v2 = self.textBrowser_2 if v1 == v2 : self.textBrowser_3.setText("Same File(s) ") else: self.textBrowser_3.setText("Different File(s) ")
-
@Stainopel said in Comparing two strings in different widgets:
v1 = self.textBrowser
v2 = self.textBrowser_2What do you expect from comparing the widgets? :-)
Try
self.textBrowser.text()
(Edit:toPlainText()
) -
@Pl45m4 The two widgets contain the results of two different button execution. These results are strings, and I want to compare the content of the widgets if they are same or different. And print the corresponding results when I click on another button to display the results of the comparison
-
@Stainopel
Hi
As @Pl45m4 says you are comparing the widgets directly.
You need to use the .text() property to get the text and compare text to text. -
@Stainopel
Ah, i checked the docs and you are right. its called
https://doc.qt.io/qt-5/qtextedit.html#toPlainText( QTextBrowser is a QTextEdit )
-
@mrjj Thanks so much for the documentation, it was a pointer in the right direction. I did find what I needed to make it work. Here is an upgrade to the code snippet, and it works real well now.
def hashComp(self): v1 = self.textBrowser.toPlainText() v2 = self.textBrowser_2.toPlainText() if v1 == v2 : self.textBrowser_3.setText("Same File(s)") else: self.textBrowser_3.setText("Different File(s)")
-
Thought this is a
QLineEdit
since they all have the same name.compare two strings in two widgets ( a text browser and a line edit)
This is why :) You said
QTextBrowser
andQLineEdit
and they are all namedtextBrowser_x