Search code examples
pythonpython-3.xctypesmessagebox

Custom Buttons in Python ctypes MessageBox


I need a python messagebox with custom buttons. This is an Edited Pic

I need something like this. I need it to return the button clicked. Like If I clicked 'A' it would return 'A'

I know about tkinter but I want to use this with pygame and can't get it to work.


Solution

  • Here is what I made with tkinter, not the perfect messagebox, but why not?

    For the theme used you have to install it first, like:

    pip install ttkthemes
    

    Then the code

    # imports
    from tkinter import *
    from tkinter import ttk
    from ttkthemes import themed_tk as tktheme
    from PIL import ImageTk, Image
    from tkinter import messagebox
    
    # making new themed window
    root = tktheme.ThemedTk()
    root.title('Take selection')
    root.get_themes()
    root.set_theme('vista')
    
    # message to be shown by the box
    message = 'Here is a custom messagebox.'
    
    # defining functions
    def click1():
        root.destroy()
        return 'A'
    
    
    def click2():
        root.destroy()
        return 'B'
    
    
    def click3():
        root.destroy()
        return 'C'
    
    
    # creating white frame
    frame1 = Frame(height=139, width=440, bg='white')
    frame1.grid(row=0, column=0)
    
    # creating gray frame
    frame2 = ttk.Frame(height=50, width=440)
    frame2.grid(row=1, column=0)
    
    # importing the image, any image can be used(for the question mark)
    dir = Image.open('Blue_question.png')
    dir = dir.resize((50, 50), Image.ANTIALIAS)
    img_prof = ImageTk.PhotoImage(dir)
    img_label = Label(root, image=img_prof, bg='white')
    img_label.grid(row=0, column=0, sticky=W, padx=25)
    
    # defining main label
    cust_messagebox = Label(root, text=message, font=('Arial', 10), bg='white')
    cust_messagebox.grid(row=0, column=0, sticky=W, padx=(95, 0))
    
    # defining buttons
    button1 = ttk.Button(root, text='A', command=click1)
    button1.grid(row=1, column=0, sticky=W, padx=30, ipadx=10)
    
    button2 = ttk.Button(root, text='B', command=click2)
    button2.grid(row=1, column=0, ipadx=10)
    
    button3 = ttk.Button(root, text='C', command=click3)
    button3.grid(row=1, column=0, sticky=E, padx=(0, 30), ipadx=10)
    
    # keeping the app alive
    root.mainloop()
    

    With bigger messages you might want to increase the width of the frames and the play with the padding of the widgets too.

    If any doubts or errors, do let me know.

    Cheers