我不知道为什么会出现NameError
错误。
代码:
from tkinter import*
from tkinter import font
import random
from tkinter import filedialog as fd
from tkinter import messagebox
def converter():
from tkinter import font
import os
num = random.choice('123456789')
alpha = random.choice('abcdefghijklmnopqrstuvwxyz')
name = num + alpha
gui_con = Tk()
gui_con.geometry("500x530")
font = font.Font(family='Helvetica', size=25, weight='bold')
address = ''
gui_con.resizable(0, 0)
gui_con.title(' CONVERTER')
gui_con.configure(bg='snow')
def choose_file():
global address
global image
address = fd.askopenfilename(parent=gui_con)
label['text'] = address
image = open(address)
def convert_file():
global image
global label
global address
top = Toplevel()
def convert_png():
global image
global address
global label
if label['text'] == '':
messagebox.showerror('ERROR', 'Select any file from above', parent=gui_con)
top.destroy()
else:
image.save(r'Converter_Img' + '\\' + name + '.png')
label['text'] = ''
address = ''
def convert_jpg():
global image
global label
global address
if label['text'] == '':
messagebox.showerror('ERROR', 'Select any file from above', parent=gui_con)
top.destroy()
else:
image.save('Converter_Img' + '\\' + name + '.jpg')
label['text'] = ''
address = ''
def convert_ico():
global image
global label
global address
if label['text'] == '':
messagebox.showerror('ERROR', 'Select any file from above', parent=gui_con)
top.destroy()
else:
image.save(r'Converter_Img' + '\\' + name + '.ico')
label['text'] = ''
address = ''
def convert_jpeg():
global image
global label
global address
if label['text'] == '':
messagebox.showerror('ERROR', 'Select any file from above', parent=gui_con)
top.destroy()
else:
image.save(r'Converter_Img' + '\\' + name + '.jpeg')
label['text'] = ''
address = ''
top.title('CONVERT')
top.geometry('300x350')
top.configure(bg='snow')
Button(top, text='Convert To PNG', font=('Helvetica', 15, 'bold'), bd=6, command=convert_png,
bg='dodger blue2').pack(pady=20)
Button(top, text='Convert To JPG', font=('Helvetica', 15, 'bold'), bd=6, command=convert_jpg,
bg='dodger blue2').pack(pady=10)
Button(top, text='Convert To ICO', font=('Helvetica', 15, 'bold'), bd=6, command=convert_ico,
bg='dodger blue2').pack(pady=20)
Button(top, text='Convert To JPEG', font=('Helvetica', 15, 'bold'), bd=6, command=convert_jpeg,
bg='dodger blue2').pack(pady=10)
top.resizable(0, 0)
top.mainloop()
def folder():
os.startfile('Converter_Img')
Label(gui_con, text='Converter', font=font, foreground='blue', bg='snow').pack(pady=40)
Button(gui_con, text='Choose File', font=font, bd=6, command=choose_file, bg='dodger blue2').pack()
label = Label(gui_con, text='', font=('Helvetica', 10, 'bold'), bg='snow', foreground='black')
label.pack(pady=20)
Button(gui_con, text='CONVERT', font=font, bd=6, command=convert_file, bg='dodger blue2').pack()
Button(gui_con, text='Output_Folder', font=font, bd=6, command=folder, bg='dodger blue2').pack(pady=50)
gui_con.mainloop()
converter()
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File"~~\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "~~/scratch_1.py", line 63, in convert_ico
if label['text'] == '':
NameError: name 'label' is not defined
预先感谢
答案 0 :(得分:0)
由于将label
变量定义为全局变量,但在全局命名空间中没有名为label
的变量,因此收到错误消息。相反,您在label
函数的范围内拥有converter()
。
答案 1 :(得分:0)
您需要考虑的是,定义全局变量还不够,还需要声明一个值。
我找不到您定义的标签行,请在脚本顶部执行label={}
并查看其是否有效