Python ctypes MessageBox 中的自定义按钮

Custom Buttons in Python ctypes MessageBox

我需要一个带有自定义按钮的 python 消息框。

我需要这样的东西。我需要它 return 单击按钮。就像如果我点击 'A' 它将 return 'A'

我知道 tkinter,但我想将其与 pygame 一起使用,但无法正常工作。

这是我用 tkinter 制作的,不是完美的消息框,但为什么不呢?

使用的主题需要先安装,如:

pip install ttkthemes

然后是代码

# 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()

对于更大的消息,您可能希望增加框架的宽度并增加小部件的填充。

如果有任何疑问或错误,请告诉我。

干杯