tkinter , PyQt5 , PyQt6 , WXPythonでダイアログボックスを表示してみます
tkinter
tkinter
from tkinter import messagebox , simpledialog
from enum import Enum
import sys
class Icon(Enum):
Information = messagebox.INFO
Question = messagebox.QUESTION
Exclamation = messagebox.WARNING
Error = messagebox.ERROR
class SimpleDialog:
@staticmethod
def askstring(title="", label="", value ='', parent=None, width=0, height=0)-> tuple[bool, str]:
ret = simpledialog.askstring(title, label, initialvalue=value)
if ret is None:
return False, ''
return True, ret
@staticmethod
def ask(title="", label="", parent = None, icon=Icon.Information)-> bool:
ret = messagebox.askokcancel(title=title ,message=label, icon=icon.value)
return ret
@staticmethod
def infomation(title="", label="", parent = None, icon=Icon.Information)-> bool:
messagebox.showinfo(title='', message=label, icon=icon.value)
return True
これがベースだったので短いコードです
PyQt5
PyQt5
from PyQt5.QtWidgets import (QInputDialog, QApplication, QMessageBox)
from PyQt5 import QtCore
from enum import Enum
import sys
class Icon(Enum):
Information : int = QMessageBox.Icon.Information
Question : int = QMessageBox.Icon.Question
Exclamation : int = QMessageBox.Icon.Warning
Error : int = QMessageBox.Icon.Critical
app = QApplication(sys.argv)
class SimpleDialog:
@staticmethod
def askstring(title="", label="",value="",parent=None, width=0, height=0)-> tuple[bool, str]:
dialog = QInputDialog()
dialog.setWindowTitle(title)
dialog.setLabelText(label)
dialog.setTextValue(value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.resize(width,0)
ret = dialog.exec() == QInputDialog.DialogCode.Accepted
return ret, dialog.textValue()
@staticmethod
def ask(parent = None, title="", label="", icon=Icon.Information)-> bool:
dialog = QMessageBox()
dialog.setWindowTitle(title)
dialog.setText(label)
dialog.setIcon(icon.value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.setStandardButtons(QMessageBox.StandardButton.Ok| QMessageBox.StandardButton.Cancel)
dialog.setDefaultButton(QMessageBox.StandardButton.Ok)
ret = dialog.exec() == QMessageBox.StandardButton.Ok
return ret
@staticmethod
def infomation(parent = None, title="", label="", icon=Icon.Information)-> bool:
dialog = QMessageBox()
dialog.setWindowTitle(title)
dialog.setText(label)
dialog.setIcon(icon.value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.setStandardButtons(QMessageBox.StandardButton.Ok)
dialog.setDefaultButton(QMessageBox.StandardButton.Ok)
ret = dialog.exec() == QMessageBox.StandardButton.Ok
return ret
長いです。その分カスタマイズの自由度も高いです
PyQt6
PyQt6
from PyQt6.QtWidgets import (QInputDialog, QApplication, QMessageBox)
from PyQt6 import QtCore
from enum import Enum
import sys
class Icon(Enum):
Information : int = QMessageBox.Icon.Information
Question : int = QMessageBox.Icon.Question
Exclamation : int = QMessageBox.Icon.Warning
Error : int = QMessageBox.Icon.Critical
app = QApplication(sys.argv)
class SimpleDialog:
@staticmethod
def askstring(title="", label="",value="",parent=None, width=0, height=0)-> tuple[bool, str]:
dialog = QInputDialog()
dialog.setWindowTitle(title)
dialog.setLabelText(label)
dialog.setTextValue(value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.resize(width,0)
ret = dialog.exec() == QInputDialog.DialogCode.Accepted.value
return ret, dialog.textValue()
@staticmethod
def ask(parent = None, title="", label="", icon=Icon.Information)-> bool:
dialog = QMessageBox()
dialog.setWindowTitle(title)
dialog.setText(label)
dialog.setIcon(icon.value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.setStandardButtons(QMessageBox.StandardButton.Ok| QMessageBox.StandardButton.Cancel)
dialog.setDefaultButton(QMessageBox.StandardButton.Ok)
ret = dialog.exec() == QMessageBox.StandardButton.Ok
return ret
@staticmethod
def infomation(parent = None, title="", label="", icon=Icon.Information)-> bool:
dialog = QMessageBox()
dialog.setWindowTitle(title)
dialog.setText(label)
dialog.setIcon(icon.value)
dialog.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
dialog.setStandardButtons(QMessageBox.StandardButton.Ok)
dialog.setDefaultButton(QMessageBox.StandardButton.Ok)
ret = dialog.exec() == QMessageBox.StandardButton.Ok
return ret
PyQt5と一見全く一緒ですが、
QInputDialog.DialogCode.Accepted
が
QInputDialog.DialogCode.Accepted.value
になっています
WXPython
WXPython
import wx
from enum import Enum
app = wx.App()
class Icon(Enum):
Information = wx.ICON_INFORMATION
Question = wx.ICON_QUESTION
Exclamation = wx.ICON_EXCLAMATION
Error = wx.ICON_ERROR
class SimpleDialog:
@staticmethod
def askstring(title="", label="",value= "" ,parent=None, width=0, height=0)-> tuple[bool, str]:
dlg = wx.TextEntryDialog(None, label,caption = title,value = value)
dlg.Size = (width, height)
dlg.WindowStyle |= wx.STAY_ON_TOP
r = dlg.ShowModal() == wx.ID_OK
v = dlg.GetValue()
dlg.Destroy()
return r, v
@staticmethod
def ask(parent = None, title="", label="", icon=Icon.Information)-> bool:
dlg = wx.MessageDialog(None, label, title, wx.OK | wx.CANCEL | icon.value)
dlg.WindowStyle |= wx.STAY_ON_TOP
r = dlg.ShowModal() == wx.ID_OK
dlg.Destroy()
return r
@staticmethod
def infomation(parent = None, title="", label="", icon=Icon.Information)-> bool:
dlg = wx.MessageDialog(None, label, title, wx.OK | icon.value)
dlg.WindowStyle |= wx.STAY_ON_TOP
dlg.ShowModal()
dlg.Destroy()
return True
特に言う事はないかも
共通部分
def main():
print(SimpleDialog.askstring(title='ににゃ',label='にににゃー',value='にゃーにゃーにゃー'))
print(SimpleDialog.ask(title='ににゃー',label='にににゃー',icon=Icon.Error))
print(SimpleDialog.infomation(title='ににゃー',label='にににゃー',icon=Icon.Question))
if __name__ == '__main__':
main()
全部このコードで動作します
オチはありません