python - Python ctypes MessageBox 中的自定义按钮
问题描述
我需要这样的东西。我需要它来返回单击的按钮。就像如果我点击'A'它会返回'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()
对于较大的消息,您可能希望增加框架的宽度以及小部件的填充。
如果有任何疑问或错误,请告诉我。
干杯
推荐阅读
- .net - 由于访问被拒绝错误而无法启动程序
- maven - 在“Maven->Update project”之后,构建路径中源文件夹的配置已更改
- java - 使用来自 RESTful Web 服务端点调用的响应,以便稍后在其他 Web 服务端点调用中使用
- excel - Excel:OR + VLOOKUP 无法检测到某些值
- java - Kotlin - 我们如何使用 getter 和 setter 访问私有财产?访问方法是否在内部调用?
- c# - 用 override 和 new 关键字覆盖 ToString()
- html - 如何在 html 模板中呈现节点 js 数据?
- python - 尽管缩进正确,但 Python 缩进错误
- swift - 在 ARKit 1.5 中重置会话时删除文本节点
- android - Android 上的硬件加速 H.264/HEVC 视频解码到 OpenGL FBO 或纹理