如何使用tkinter消息框返回函数的结果

时间:2019-04-30 03:29:32

标签: python tkinter

我正在尝试编写返回头尾的代码,并将其显示在单独的窗口(消息框)中。如何在单独的窗口(消息框)中显示函数的结果?

import tkinter
import numpy as np
from tkinter import messagebox

result = []

def hola():

    coin = np.random.randint(0,2)
    if coin == 0:
       result.append("heads")
    else:
       result.append("tails")
    messagebox.showinfo( "Lucky you !", result )

top = tkinter.Tk()
B = tkinter.Button(top, text ="flip da coin", command = hola)

B.pack()
top.mainloop()

1 个答案:

答案 0 :(得分:0)

我认为您想做这样的事情;

import tkinter
import numpy as np
from tkinter import messagebox

def hola():

    coin = np.random.randint(0,2)
    if coin == 0:
       result = "heads"
    else:
       result = "tails"
    messagebox.showinfo( "Lucky you !", result )

top = tkinter.Tk()
B = tkinter.Button(top, text ="flip da coin", command = hola)

B.pack()
top.mainloop()