已将.py文件移到另一个文件夹,现在仅在新文件夹中出现TKinter错误

2024-04-26 21:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python自动化一些分析过程。目前,我有一个程序,它只通过它所在的目录,计算CSV和XLSX文档的数量,并提示用户输入一个密码,他/她想用来加密它们。实际的加密将由一个我还没来得及下载的库来完成,但在确保文件正常工作的同时,我将它移到了另一个文件夹,并遇到了一个新的错误,我不知道如何解析。你知道吗

程序如下:

#import pyminizip - lib to download later

import tkinter

from tkinter import messagebox

import os

class whatisthepassword:

    def __init__ (self):

        self.root = tkinter.Tk()

        w = self.root.winfo_screenwidth()

        h = self.root.winfo_screenwidth()

        self.root.geometry("+%d+%d" % ( 200,200) )

        self.root.wm_title("Amir's ZIP & Encrypt shortcut")

        self.frame = tkinter.Frame(self.root)

        self.button_frame = tkinter.Frame(self.root)

        self.pwfield_label = tkinter.Label(self.frame, text = "Password to encrypt files with:", font = 20, width = 25, borderwidth = 2,anchor=tkinter.W)

        self.pwfield = tkinter.Entry(self.frame ,font=20, width = 25, relief = "groove", borderwidth = 2)

        self.begin=tkinter.Button(self.button_frame, text='ZIP & Encrypt', font=20, width=50, command = lambda: self.getpw())

        self.pwfield_label.grid(row=0, column = 0)

        self.pwfield.grid(row = 0, column = 1)  

        self.begin.grid()

        self.frame.pack()

        self.button_frame.pack()

        self.root.mainloop()

    def getpw(self):

        while True:

            pw = (self.pwfield.get())

            if pw == '':

                messagebox.showinfo("Uh oh", "Please enter a password")

                break

            else:

                zipcrypt(pw)

                self.root.destroy()



def zipcrypt(pw):

    folder = os.listdir(os.getcwd())

    numcsvs = 0 # number of csvs

    numxlsx = 0

    for file in folder:

            if '.csv' in file:

                numcsvs += 1

            if '.xlsx' in file:

                numxlsx += 1

    if numcsvs > 1:

        messagebox.showinfo("The Double Do", "You are about to \n\n- ZIP and Encrypt %d .csv file(s) \n- Password Protect %d .xlsx file(s)  \n\nUsing the passphrase \'%s\'\n\nContinue?" % (numcsvs, numxlsx, pw))

def main():

    passwordbox = whatisthepassword()

main()

这就是错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\aj180\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:\Users\aj180\Documents\To Automate\To Automate\create_new_password_zip_folder.py", line 39, in <lambda>
    self.begin=tkinter.Button(self.button_frame, text='ZIP & Encrypt', font=20, width=50, command = lambda: self.getpw())
  File "C:\Users\aj180\Documents\To Automate\To Automate\create_new_password_zip_folder.py", line 57, in getpw
    pw = (self.pwfield.get())
  File "C:\Users\aj180\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2521, in get
    return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".49912240.49912368"

我通常在最后两行得到不同的十进制值。 当我移动文件时,它会改变吗?这个错误是什么意思?你知道吗

还有,有没有更好的方法来格式化我的代码?你知道吗

谢谢你们。你知道吗


Tags: inimportselftkinterdefbuttonrootzip
1条回答
网友
1楼 · 发布于 2024-04-26 21:29:51

这是因为在它周围有一个while True循环-你得到了pwfield的内容,如果它们是好的,你就去你的else,在你的else中你破坏了根,并扩展了它的所有子级。十进制值是小部件的Tcl标识符,由于您销毁了小部件,因此不再是可行的命令。你知道吗

老实说,这个循环是完全不相关的(不惜一切代价避免GUI应用程序中的while-True循环,因为它们会阻止GUI的更新,使其没有响应),因为if成功时会终止循环,if失败时会导致严重错误。你知道吗

相关问题 更多 >