tkinter"无法调用wm命令应用程序已被销毁"

时间:2016-08-04 15:30:06

标签: python tkinter

我制作了一个用于阅读和修改Excel文件的脚本。

如果我在代码中写入文件全名,工作流程很好,但我想用多个Excel文件来完成这项工作。所以我添加了一个带有python模块 tkinter.filedialog 的对话框。

现在我已经发出了这样的信息:"无法调用wm命令应用程序已被销毁"

当我点击'有效'当我开始启动def代码()时,会出现此消息。盒子的按钮。

这是我的代码:

import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import askopenfilename
import openpyxl
from openpyxl import *
from openpyxl.utils import get_column_letter

inputbox = Tk()
inputbox.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("Fichier Excel", "*.xlsx;*.xls"),("Fichier CSV", "*.csv"),("All files", "*.*")])
var_path = str(file_path)

wb= Workbook()
wb= openpyxl.load_workbook(var_path)

# On active l'onglet courant et on le renomme
ws = wb.active
var_titre = ws.title
ws.title = 'old'

# On crée un nouvel onglet et on le renomme
ws1 = wb.create_sheet()
ws1.title = var_titre

# Préparation des fenêtres d'interface utilisateur
def code():
    print(Code.get())
    if Code.get() == '':
        showwarning('Résultat','Code incorrect.\nVeuillez recommencer !')
        Code.set('')
    else:
        var_code_insee=int(Code.get())
        ws.cell(column=var_col, row=row, value=var_code_insee)
        inputbox.destroy()

# On s'apprête à parcourir la colonne B
begrow = 1
endrow = ws.max_row
endrows1 = 0
endrows1_incrementation = 0
for row in range(begrow,endrow):
    if endrows1 > 1:
        endrows1_incrementation = endrows1 - 5
        endrows1 = endrows1_incrementation

    # CAS 1.1 : Le code n'est pas renseigné dans la colonne A alors, on demande à l'utilisateur de le renseigner
    if ws['A' + format(row)].value is None and ws['B' + format(row)].value is not None:
            # On gére les paramétre de la boîte de dialogue
            inputbox.deiconify()
            inputbox.title('Code non renseigné')

            #Déclaration des variables nécessaires au traitement
            var_col = 1

            # Création d'un Label
            Label1 = Label(inputbox, text='Code de : ' + ws['B' + format(row)].value)
            Label1.pack(side = LEFT, padx = 5, pady = 5)

            # Création du champ de saisie
            Code= StringVar()
            Champ = Entry(inputbox,textvariable= Code, bg ='bisque', fg='maroon')
            Champ.focus_set()
            Champ.pack(side = LEFT, padx = 5, pady = 5)

            #Création d'un bouton de Validation
            Bouton = Button(inputbox, text ='Valider', command = code)
            Bouton.pack(side = LEFT, padx = 5, pady = 5)

            inputbox.mainloop()

0 个答案:

没有答案