将tkinter与消息框一起使用时,会有额外的tkinter框

时间:2018-11-17 09:15:59

标签: tkinter python-3.6

我的代码是:

import numpy as np
import matplotlib.pyplot as plt
import csv
from tkinter import messagebox
messagebox.showinfo("welcome","welcome to piechart creator!!!")
try:
    x=input("enter the file to be fetched:")
    outfile = open(x,"r")
except:
    messagebox.showinfo("alert","enter the file in csv extension!!!")
else:
    file=csv.reader(outfile)
    #skip the headers
    next(file, None)
    row1=input("enter the title of first label:" )
    row2=input("enter the title of label2:")
    row1= []
    row2= []
    for row in file:
        row1.append(row[0])
        row2.append(row[1])
    plt.pie(row2, labels=row1)
    plt.axis('equal')
    messagebox.askyesno("question","do you want the piechart to be created???")
    plt.show()
finally:
    messagebox.showinfo("message","thank you for using piechart creator.")

我有一个额外的tkinter盒子-为什么?

1 个答案:

答案 0 :(得分:0)

如果您未明确创建Tk()的实例,则tkinter将为您完成此操作。您可以通过在脚本的开头显式创建根窗口,然后将其隐藏来解决此问题:

import tkinter as tk
root = tk.Tk()     # Create window
root.withdraw()    # Hide window