Question

I'm trying to work on a GUI done in pyqt. I'm trying to create a pop-up window has a textbox where a user can type/set the user's id (1-99) and then click an 'ok' button to set it and close the window. This is what I have so far.

def viewProfile(self)
        profBox = QMessageBox()
        QMessageBox.about(self, 'Profile', "///Text box where can type User ID:// ",
    QMessageBox.Ok)

I am not sure how to generate the textbox.

Also, if I want to display the integer value or string of a variable in my message window /box do I just leave it out of quotation marks but include it? What's the syntax for it?

Thank you!

No correct solution

OTHER TIPS

You want to use a QInputDialog. This has a bunch of static methods which generate a complete dialog, and return the selected integer when the user clicks OK. This means you don't need to worry about creating a dialog object, adding widgets and buttons, etc.

So you would want to call:

parent_window = self #probably..., depends on your code
minimum_value = 1
maximum_value = 99
default_value = 1
title = "Profile"
message = "Select your user ID"
user_id, ok = QInputDialog.getInt(parent_window, title, message, default_value, minimum_value, maximum_value)

When the QInputDialog line of code runs, a dialog will be presented to the user. When the user clicks OK or Cancel, the entered user_ID will be placed in user_id and ok will be a boolean value that indicates whether the OK button was clicked (True if the OK button was clicked, False if the cancel button was clicked)

If you want to place an integer in the message, you could do something like:

message = "Select your user ID. An integer I want you to know about is %d. I hope you find that useful."%my_integer

But that is really a Python string formatting question, which you should research separately. In short, in my example you can display one string. How long, that string is, is up to you (it can be multiple lines, have new line characters, etc)

You should use QDialog. That way you can customize it the way you want (add textbox, button...) Take a look at my answer here, basically it's log-in dialog created in QTDesigner, but you can create it with code since it's way more simplier

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top