为了账号安全,请及时绑定邮箱和手机立即绑定

Python ctypes MessageBox 中的自定义按钮

Python ctypes MessageBox 中的自定义按钮

喵喵时光机 2023-05-09 09:57:41
我需要一个带有自定义按钮的 python 消息框。.我需要这样的东西。我需要它来返回单击的按钮。就像如果我点击'A'它会返回'A'我知道 tkinter,但我想将它与 pygame 一起使用,但无法让它工作。
查看完整描述

1 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

这是我用 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()

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


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


查看完整回答
反对 回复 2023-05-09
  • 1 回答
  • 0 关注
  • 179 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信