In this tutorial, we are going to talk about adding an input box and getting input values using PyQt5. Sometimes you need to get an input value from your GUI software in tasks like a messenger app, login interface, and much more stuff. So, In PyQt5 you can make it easy with a higher appearance. In this post, you can learn below subtopics,
1. Creating an Input Box
2. Adding CSS styles to customize it
3. Setting a default text inside it
4. Getting an Input Value from it
Creating the Input Box
You can easily create an input box object inside the PyQt5 window using the QLineEdit() function. You can get an idea about it by referring below code
QLineEdit(self) -- this is the built-in function create a input box in PyQt5
setGeometry() -- I'm using this to adjust the input box location in the window and its size.
If you try that code in your IDE you'll see that you can type text inside the Input Box we created.
Adding CSS styles to make it Attractive
Using the above code, we create an input box, but it doesn't have any attractive look, we can use CSS styles to avoid that problem.
self.input_test.setStyleSheet("background: #FAFAD2;"
"color: #800080;"
"font-size: 20px;"
"border: 3px solid #F4A460;"
"border-radius: 10px;")
"color: #800080;"
"font-size: 20px;"
"border: 3px solid #F4A460;"
"border-radius: 10px;")
in my CSS style color: #800080 is the color of text, and "background: #FAFAD2" is the color of the background in my input box. I mentioned that because you can misunderstand those parts. If still can't understand my CSS you can comment below. Also, you can refer to the PyQt5 button post I have done.
How to Set a default Text inside Input Box
Now we have a beautiful Input Box, However, you may know that some Input Box has a default text inside it with showing a command. You also have that feature inside the PyQt5. You can simply use the below command to make it.
self.input_test.setText("Click to Type")
Getting an Input Value from the Input Box
Now we have an Input Box complete with every single functionality. But how do you get a value that someone entered into the Input Box? Well, You can do it in a simple step.
input_value = self.input_test.text()
print(input_value)
If you follow my steps, you'll face a problem. You may already see that the program only prints our default text value. because our program doesn't have an option to reload the value printing function back when we need it. You can easily avoid that problem using a button.
self.my_button = QPushButton("Submit", self)
self.my_button.setGeometry(245, 250, 100, 50)
self.my_button.setStyleSheet("*{background:#FF1493;" +
"color: #000;" +
"font-size: 20px;" +
"border: 1px solid #000;" +
"border-radius: 5px;}")
self.my_button.setCursor(QCursor(Qt.PointingHandCursor))
self.my_button.clicked.connect(self.get_value) -- This is the function to connect our button to a task we need to do.
def get_value(self):
input_value = self.input_test.text()
print(input_value)
If you don't have good knowledge about PyQt5 buttons, you can refer to the post I have done about everything in buttons.
I hope you guys found answers to your questions and get knowledge through this post. If you have a problem regarding this post or any other problem about PyQt5 you can comment below.
0 Comments