Skip to content

Instantly share code, notes, and snippets.

@s-nako
Created April 12, 2021 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-nako/1821d3c25997ec4a868f1e5f0bd2f687 to your computer and use it in GitHub Desktop.
Save s-nako/1821d3c25997ec4a868f1e5f0bd2f687 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*
import sys
from PySide2.QtCore import Qt, QEventLoop
from PySide2.QtWidgets import *
class MainUi(QWidget):
def __init__(self, parent):
super(MainUi, self).__init__(parent)
main_layout = QHBoxLayout()
self.setLayout(main_layout)
button1 = QPushButton("getText")
button1.clicked.connect(self.show_get_text_dialog)
main_layout.addWidget(button1)
button2 = QPushButton("getInt")
button2.clicked.connect(self.show_get_int_dialog)
main_layout.addWidget(button2)
button3 = QPushButton("getDouble")
button3.clicked.connect(self.show_get_double_dialog)
main_layout.addWidget(button3)
button4 = QPushButton("getItem")
button4.clicked.connect(self.show_get_item_dialog)
main_layout.addWidget(button4)
def show_get_text_dialog(self):
name, ok = QInputDialog.getText(self, 'Name form', 'input name:')
if ok:
print("input is ", name)
def show_get_int_dialog(self):
age, ok = QInputDialog.getInt(self, "age", "input age:", minValue=0)
if ok:
print("input is ", age)
def show_get_double_dialog(self):
height, ok = QInputDialog.getDouble(self, "height", "input height:", minValue=0)
if ok:
print("input is ", height)
def show_get_item_dialog(self):
selected_class, ok = QInputDialog.getItem(self, "class", "select class", ["math", "science", "history"])
if ok:
print("input is ", selected_class)
# Create a Qt application
app = QApplication(sys.argv)
main = MainUi(None)
main.show()
app.exec_()
sys.exit()
@high-solutions
Copy link

high-solutions commented Nov 15, 2022

this just doesn't work on python 3.9.
This script stalls and gives no errors.

@s-nako
Copy link
Author

s-nako commented Nov 18, 2022

I tried on Python3.9.13, pyside2-5.15.2.1, shiboken2-5.15.2.1 and Windows10
It works well.
image

So could you give me some more information? What happens if you try "python input_dialog_example.py" on command prompt?

@high-solutions
Copy link

I suppose it has to do something with my system, can't get it to work on my mac 12.3.1 (monterey).
It shows a blank window and after a while i'm getting a python crash, this exact code runs fine with PySide6 or pyQt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment