Skip to content

Commit

Permalink
work towards making compatible with PyQt5
Browse files Browse the repository at this point in the history
  • Loading branch information
aroberge committed Dec 25, 2017
1 parent 187f5b4 commit a85db15
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 131 deletions.
19 changes: 11 additions & 8 deletions easygui_qt/calendar_widget.py
@@ -1,30 +1,33 @@

try:
from PyQt4 import QtGui, QtCore
qt_widgets = QtGui
except ImportError:
from PyQt5 import QtGui, QtCore # untested
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets as qt_widgets

class CalendarWidget(QtGui.QWidget):

class CalendarWidget(qt_widgets.QWidget):
"""Creates a calendar widget allowing the user to select a date."""
def __init__(self, title="Calendar"):
super(CalendarWidget, self).__init__()

self.setWindowTitle(title)
layout = QtGui.QGridLayout()
layout = qt_widgets.QGridLayout()
layout.setColumnStretch(1, 1)

self.cal = QtGui.QCalendarWidget(self)
self.cal = qt_widgets.QCalendarWidget(self)
self.cal.setGridVisible(True)
self.cal.clicked[QtCore.QDate].connect(self.show_date)
layout.addWidget(self.cal, 0, 0, 1, 2)

self.date_label = QtGui.QLabel()
self.date_label = qt_widgets.QLabel()
self.date = self.cal.selectedDate()
self.date_label.setText(self.date.toString())
layout.addWidget(self.date_label, 1, 0)

button_box = QtGui.QDialogButtonBox()
confirm_button = button_box.addButton(QtGui.QDialogButtonBox.Ok)
button_box = qt_widgets.QDialogButtonBox()
confirm_button = button_box.addButton(qt_widgets.QDialogButtonBox.Ok)
confirm_button.clicked.connect(self.confirm)
layout.addWidget(button_box, 1, 1)

Expand All @@ -42,7 +45,7 @@ def confirm(self):

if __name__ == '__main__':
import sys
app = QtGui.QApplication([])
app = qt_widgets.QApplication([])
cal = CalendarWidget(title="title")
app.exec_()
date = cal.date.toString()
Expand Down
22 changes: 12 additions & 10 deletions easygui_qt/demos/launcher.py
Expand Up @@ -12,8 +12,10 @@

try:
from PyQt4 import QtGui, QtCore
qt_widgets = QtGui
except ImportError:
from PyQt5 import QtGui, QtCore # untested
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets as qt_widgets

def launch(name, *args):
"""Executes a script designed specifically for this launcher.
Expand All @@ -38,14 +40,14 @@ def launch(name, *args):
return output


class Dialog(QtGui.QDialog):
class Dialog(qt_widgets.QDialog):

def __init__(self, parent=None):
flags = QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint
super(Dialog, self).__init__(parent, flags=flags)

frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
layout = QtGui.QGridLayout()
frameStyle = qt_widgets.QFrame.Sunken | qt_widgets.QFrame.Panel
layout = qt_widgets.QGridLayout()
layout.setColumnStretch(1, 1)
layout.setColumnMinimumWidth(1, 250)

Expand All @@ -71,17 +73,17 @@ def __init__(self, parent=None):
'get_language', 'set_font_size',
'show_file', 'show_code', 'get_abort', 'find_help']
for n, fxn in enumerate(fxns):
self.button[fxn] = QtGui.QPushButton(fxn + "()")
self.button[fxn] = qt_widgets.QPushButton(fxn + "()")
self.button[fxn].clicked.connect(getattr(self, fxn))
self.button[fxn].setToolTip(getattr(easygui_qt, fxn).__doc__)
self.label[fxn] = QtGui.QLabel()
self.label[fxn] = qt_widgets.QLabel()
self.label[fxn].setFrameStyle(frameStyle)
layout.addWidget(self.button[fxn], n, 0)
layout.addWidget(self.label[fxn], n, 1)

# handle special-case display items separately:
n += 1
self.python_version_label = QtGui.QLabel()
self.python_version_label = qt_widgets.QLabel()
layout.addWidget(self.python_version_label, n, 0, 2, 2)
output = subprocess.check_output(
['python', '-c', "import sys;print(sys.version)"])
Expand All @@ -90,11 +92,11 @@ def __init__(self, parent=None):

n += 2

self.cancel_btn = QtGui.QPushButton("Quit")
self.cancel_btn = qt_widgets.QPushButton("Quit")
self.cancel_btn.clicked.connect(self.close)
layout.addWidget(self.cancel_btn, n, 0)

self.handle_exception_label = QtGui.QLabel()
self.handle_exception_label = qt_widgets.QLabel()
self.handle_exception_label.setToolTip(
easygui_qt.handle_exception.__doc__)
self.handle_exception_label.setText(" handle_exception() not shown" +
Expand Down Expand Up @@ -220,7 +222,7 @@ def find_help(self):
launch('find_help')

def main():
_ = QtGui.QApplication([])
_ = qt_widgets.QApplication([])
dialog = Dialog()
dialog.exec_()

Expand Down
77 changes: 43 additions & 34 deletions easygui_qt/easygui_qt.py
Expand Up @@ -16,8 +16,12 @@

try:
from PyQt4 import QtGui, QtCore
qt_widgets = QtGui
_qt4 = True
except ImportError:
from PyQt5 import QtGui, QtCore # untested
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets as qt_widgets
_qt4 = False


try:
Expand Down Expand Up @@ -71,7 +75,7 @@
QM_FILES = None


class SimpleApp(QtGui.QApplication):
class SimpleApp(qt_widgets.QApplication):
"""A simple extention of the basic QApplication
with added methods useful for working with dialogs
that are not class based.
Expand Down Expand Up @@ -173,7 +177,7 @@ def show_message(message="Message",
.. image:: ../docs/images/show_message.png
"""
app = SimpleApp()
box = QtGui.QMessageBox(None)
box = qt_widgets.QMessageBox(None)
box.setWindowTitle(title)
box.setText(message)
box.show()
Expand All @@ -197,16 +201,16 @@ def get_yes_or_no(message="Answer this question", title="Title"):
.. image:: ../docs/images/yes_no_question.png
"""
app = SimpleApp()
flags = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
flags |= QtGui.QMessageBox.Cancel
flags = qt_widgets.QMessageBox.Yes | qt_widgets.QMessageBox.No
flags |= qt_widgets.QMessageBox.Cancel

box = QtGui.QMessageBox()
box = qt_widgets.QMessageBox()
box.show()
box.raise_()

reply = box.question(None, title, message, flags)
app.quit()
return reply == QtGui.QMessageBox.Yes
return reply == qt_widgets.QMessageBox.Yes


def get_continue_or_cancel(message="Processed will be cancelled!",
Expand All @@ -229,16 +233,16 @@ def get_continue_or_cancel(message="Processed will be cancelled!",
.. image:: ../docs/images/get_continue_or_cancel.png
"""
app = SimpleApp()
message_box = QtGui.QMessageBox(QtGui.QMessageBox.Warning, title, message,
QtGui.QMessageBox.NoButton)
message_box.addButton(continue_button_text, QtGui.QMessageBox.AcceptRole)
message_box.addButton(cancel_button_text, QtGui.QMessageBox.RejectRole)
message_box = qt_widgets.QMessageBox(qt_widgets.QMessageBox.Warning, title, message,
qt_widgets.QMessageBox.NoButton)
message_box.addButton(continue_button_text, qt_widgets.QMessageBox.AcceptRole)
message_box.addButton(cancel_button_text, qt_widgets.QMessageBox.RejectRole)
message_box.show()
message_box.raise_()

reply = message_box.exec_()
app.quit()
return reply == QtGui.QMessageBox.AcceptRole
return reply == qt_widgets.QMessageBox.AcceptRole


#============= Color dialogs =================
Expand All @@ -253,7 +257,7 @@ def get_color_hex():
.. image:: ../docs/images/select_color.png
"""
app = SimpleApp()
color = QtGui.QColorDialog.getColor(QtCore.Qt.white, None)
color = qt_widgets.QColorDialog.getColor(QtCore.Qt.white, None)
app.quit()
if color.isValid():
return color.name()
Expand All @@ -269,7 +273,7 @@ def get_color_rgb(app=None):
.. image:: ../docs/images/select_color_fr.png
"""
app = SimpleApp()
color = QtGui.QColorDialog.getColor(QtCore.Qt.white, None)
color = qt_widgets.QColorDialog.getColor(QtCore.Qt.white, None)
app.quit()
if color.isValid():
return (color.red(), color.green(), color.blue())
Expand Down Expand Up @@ -358,7 +362,7 @@ def get_common_input_flags():
flags |= QtCore.Qt.WindowStaysOnTopHint
return flags

class VisibleInputDialog(QtGui.QInputDialog):
class VisibleInputDialog(qt_widgets.QInputDialog):
'''A simple InputDialog class that attempts to make itself automatically
on all platforms
'''
Expand Down Expand Up @@ -412,9 +416,14 @@ def get_int(message="Choose a number", title="Title",
app = SimpleApp()
dialog = VisibleInputDialog()
flags = get_common_input_flags()
number, ok = dialog.getInteger(None, title, message,
if _qt4:
number, ok = dialog.getInteger(None, title, message,
default_value, min_, max_, step,
flags)
else:
number, ok = dialog.getInt(None, title, message,
default_value, min_, max_, step,
flags)
dialog.destroy()
app.quit()
if ok:
Expand Down Expand Up @@ -487,7 +496,7 @@ def get_string(message="Enter your response", title="Title",
app = SimpleApp()
dialog = VisibleInputDialog()
flags = get_common_input_flags()
text, ok = dialog.getText(None, title, message, QtGui.QLineEdit.Normal,
text, ok = dialog.getText(None, title, message, qt_widgets.QLineEdit.Normal,
default_response, flags)
app.quit()
if ok:
Expand All @@ -514,7 +523,7 @@ def get_password(message="Enter your password", title="Title"):
app = SimpleApp()
dialog = VisibleInputDialog()
flags = get_common_input_flags()
text, ok = dialog.getText(None, title, message, QtGui.QLineEdit.Password,
text, ok = dialog.getText(None, title, message, qt_widgets.QLineEdit.Password,
'', flags)
app.quit()
if ok:
Expand Down Expand Up @@ -702,13 +711,13 @@ def get_directory_name(title="Get directory"):
working directory.
'''
app = SimpleApp()
options = QtGui.QFileDialog.Options()
options = qt_widgets.QFileDialog.Options()
# Without the following option (i.e. using native dialogs),
# calling this function twice in a row made Python crash.
options |= QtGui.QFileDialog.DontUseNativeDialog
options |= QtGui.QFileDialog.DontResolveSymlinks
options |= QtGui.QFileDialog.ShowDirsOnly
directory = QtGui.QFileDialog.getExistingDirectory(None,
options |= qt_widgets.QFileDialog.DontUseNativeDialog
options |= qt_widgets.QFileDialog.DontResolveSymlinks
options |= qt_widgets.QFileDialog.ShowDirsOnly
directory = qt_widgets.QFileDialog.getExistingDirectory(None,
title, os.getcwd(), options)
app.quit()
if sys.version_info < (3,):
Expand All @@ -733,13 +742,13 @@ def get_file_names(title="Get existing file names"):
'''
app = SimpleApp()
if sys.version_info < (3,):
files = QtGui.QFileDialog.getOpenFileNames(None, title, os.getcwd(),
files = qt_widgets.QFileDialog.getOpenFileNames(None, title, os.getcwd(),
"All Files (*.*)")
files = [unicode(item) for item in files]
else:
options = QtGui.QFileDialog.Options()
options |= QtGui.QFileDialog.DontUseNativeDialog
files = QtGui.QFileDialog.getOpenFileNames(None, title, os.getcwd(),
options = qt_widgets.QFileDialog.Options()
options |= qt_widgets.QFileDialog.DontUseNativeDialog
files = qt_widgets.QFileDialog.getOpenFileNames(None, title, os.getcwd(),
"All Files (*.*)", options)
app.quit()
return files
Expand All @@ -765,14 +774,14 @@ def get_save_file_name(title="File name to save"):
'''
app = SimpleApp()
if sys.version_info < (3,):
file_name = QtGui.QFileDialog.getSaveFileName(None, title, os.getcwd(),
file_name = qt_widgets.QFileDialog.getSaveFileName(None, title, os.getcwd(),
"All Files (*.*)")
app.quit()
return unicode(file_name)

options = QtGui.QFileDialog.Options()
options |= QtGui.QFileDialog.DontUseNativeDialog # see get_directory_name
file_name = QtGui.QFileDialog.getSaveFileName(None, title, os.getcwd(),
options = qt_widgets.QFileDialog.Options()
options |= qt_widgets.QFileDialog.DontUseNativeDialog # see get_directory_name
file_name = qt_widgets.QFileDialog.getSaveFileName(None, title, os.getcwd(),
"All Files (*.*)", options)
app.quit()
return file_name
Expand Down Expand Up @@ -899,9 +908,9 @@ def get_abort(message="Major problem - or at least we think there is one...",
'''

app = SimpleApp()
reply = QtGui.QMessageBox.critical(None, title, message,
QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore)
if reply == QtGui.QMessageBox.Abort:
reply = qt_widgets.QMessageBox.critical(None, title, message,
qt_widgets.QMessageBox.Abort | qt_widgets.QMessageBox.Ignore)
if reply == qt_widgets.QMessageBox.Abort:
sys.exit()
else:
pass
Expand Down
24 changes: 13 additions & 11 deletions easygui_qt/language_selector.py
Expand Up @@ -6,10 +6,12 @@

try:
from PyQt4 import QtGui, QtCore
qt_widgets = QtGui
except ImportError:
from PyQt5 import QtGui, QtCore # untested
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets as qt_widgets

class LanguageSelector(QtGui.QDialog):
class LanguageSelector(qt_widgets.QDialog):
"""A specially constructed dialog which uses informations about
available language (qm) files which can be used to change the
default language of the basic PyQt ui components.
Expand All @@ -26,18 +28,18 @@ def __init__(self, parent, title="Language selection",
qm_files = utils.find_qm_files()

# ========= check boxes ==============
group_box = QtGui.QGroupBox(name)
group_box_layout = QtGui.QGridLayout()
group_box = qt_widgets.QGroupBox(name)
group_box_layout = qt_widgets.QGridLayout()
for i, locale in enumerate(qm_files):
check_box = QtGui.QCheckBox(locale)
check_box = qt_widgets.QCheckBox(locale)
check_box.setAutoExclusive(True)
self.qm_files_choices[check_box] = locale
check_box.toggled.connect(self.check_box_toggled)
group_box_layout.addWidget(check_box, i / 4, i % 4)
# adding default language option. When using the PyQt distribution
# no "en" files were found and yet "en" was the obvious default.
# We need this option in case we want to revert a change.
check_box = QtGui.QCheckBox("Default")
check_box = qt_widgets.QCheckBox("Default")
check_box.setAutoExclusive(True)
self.qm_files_choices[check_box] = "default"
check_box.toggled.connect(self.check_box_toggled)
Expand All @@ -46,14 +48,14 @@ def __init__(self, parent, title="Language selection",
group_box.setLayout(group_box_layout)

# ========= buttons ==============
button_box = QtGui.QDialogButtonBox()
confirm_button = button_box.addButton(QtGui.QDialogButtonBox.Ok)
button_box = qt_widgets.QDialogButtonBox()
confirm_button = button_box.addButton(qt_widgets.QDialogButtonBox.Ok)
confirm_button.clicked.connect(self.confirm)

# ========= finalizing layout ====
main_layout = QtGui.QVBoxLayout()
main_layout = qt_widgets.QVBoxLayout()
main_layout.addWidget(group_box)
main_layout.addWidget(QtGui.QLabel(instruction))
main_layout.addWidget(qt_widgets.QLabel(instruction))
main_layout.addWidget(button_box)
self.setLayout(main_layout)
self.setWindowTitle(title)
Expand All @@ -72,7 +74,7 @@ def confirm(self):
self.close()

if __name__ == '__main__':
app = QtGui.QApplication([])
app = qt_widgets.QApplication([])
# mocks
app.config = {'locale': None}
app.set_locale = lambda x: x
Expand Down

0 comments on commit a85db15

Please sign in to comment.