1

Using this answer from How can I create a simple message box in Python?, I have created a Yes/No/Cancel Popup box:

>>> import ctypes
>>> ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 3)

Which looks like this:

enter image description here

I was wondering if you could change the text of the buttons from the defaults "Yes", "No" and "Cancel"? I know I could use tkinter to do this, but is their a quick workaround with this ctypes implementation?

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 2
    My prediction is that to do this, you will find yourself in win32 api hell. It will probably be easier to use a python gui library and your solution will be cross platform to boot. As far as tkinter goes there is [this](http://effbot.org/tkinterbook/tkinter-dialog-windows.htm) (although be warned effbot is dated). – Paul Rooney May 15 '18 at 03:52
  • 1
    `MessageBox` offers a limited set of buttons (with their texts), available through arguments. Do you want to change only that, or place any text on those buttons? – CristiFati May 15 '18 at 05:50

1 Answers1

2

I think @Paul Rooney has a very good point that tkinter will be cross platform. And there is a little bit more overhead than one might like to call a message box.

Looking at the MessageBox documentation from Microsoft (MessageBoxW is the unicode version of MessageBox), it seems you have a set number of options for what the buttons can be and that is determined by the 4th argument in the function call:

MB_ABORTRETRYIGNORE = 2
MB_CANCELTRYCONTINUE = 6
MB_HELP = 0x4000 = 16384
MB_OK   = 0
MB_OKCANCEL = 1
MB_RETRYCANCEL = 5
MB_YESNO = 4
MB_YESNOCANCEL = 3

If these choices are good for you and you're strictly Windows, this could be a winner for you. It's nice because you only have the ctypes import and the actual function call. Though to be a little safer you should consider using the argtypes function from ctypes to make a function prototype.

To do it the tkinter way, you still have most of the same options for a simple message box (e.g. Yes/No, OK/Cancel, etc). If you really need to control the button text, then you'll have to layout a basic form. Here's a basic example of making your own form. I think you'll find it quite tedious.

from tkinter import Tk, LEFT, RIGHT, BOTH, RAISED, Message
from tkinter.ttk import Frame, Button, Style, Label


class Example(Frame):

    def __init__(self):
        super().__init__()   

        self.initUI()


    def initUI(self):

        self.master.title("Buttons")
        self.style = Style()
        self.style.theme_use("default")

        frame = Frame(self, relief=RAISED, borderwidth=1)

        message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua... '

        lbl1 = Message(frame, text=message)
        lbl1.pack(side=LEFT, padx=5, pady=5) 

        frame.pack(fill=BOTH, expand=True)

        self.pack(fill=BOTH, expand=True)

        button1 = Button(self, text="button1")
        button1.pack(side=RIGHT, padx=5, pady=5)
        button2 = Button(self, text="second button")
        button2.pack(side=RIGHT)


def main():

    root = Tk()
    root.geometry("300x200+300+300")
    app = Example()
    root.mainloop()  

if __name__ == '__main__':
    main() 
bfris
  • 5,272
  • 1
  • 20
  • 37