首页 > 解决方案 > 使用带有消息框的 tkinter 时额外的 tkinter 框

问题描述

我的代码是:

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 盒子——这是为什么呢?

标签: tkinterpython-3.6

解决方案


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

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

推荐阅读