开发环境:
设备:MacBook Air
版本:Python 3.7
创建按钮代码:
import tkinter.messagebox
top = tkinter.Tk()
def helloCallback():
tkinter.messagebox.askokcancel("提示", "Hello,World")
B = tkinter.Button(top, text="点我", command=helloCallback)
B.pack()
top.mainloop()
执行后按钮是出来了,点击后也弹出来框了,但是按钮文字不显示,百度一下答案就出来了
from tkinter import ttk
import tkinter.messagebox
top = tkinter.Tk()
def helloCallback():
tkinter.messagebox.askokcancel("提示", "Hello,World")
B = ttk.Button(top, text="点我", command=helloCallback)
B.pack()
top.mainloop()
补充:
给按钮加图片
from tkinter import ttk
import tkinter.messagebox
from PIL import ImageTk
top = tkinter.Tk()
def helloCallback():
tkinter.messagebox.askokcancel("提示", "Hello,World")
img = ImageTk.PhotoImage(file="/data/image/未标题-1.jpg")
B = ttk.Button(top, text="点我", command=helloCallback, width=200, image=img)
B.pack()
top.mainloop()