我一直在尝试解决该错误:
ImportError:没有名为tkinter的模块
但是,以前的问题似乎都没有解决方案。
我尝试过:
sudo apt-get install python3-tk
并成功安装了tkinter,但问题仍然存在。
由于我使用的是Python 3.8.2,因此我尝试将t
中的tkinter
大写,但这没有用。
我还尝试过重新安装/修复Python,因为tkinter
应该是最新的Python版本。
尝试使用python3 main.py
打开文件会导致以下错误
回溯(最近通话最近):
中的文件“ main.py”,第95行 窗口= Tk()
在 init
中的文件“ /usr/lib/python3.6/tkinter/init.py”,第2023行 self.tk = _tkinter.create(screenName,baseName,className,交互式,wantobjects,useTk,sync,use)
_tkinter.TclError:没有显示名称,也没有$ DISPLAY环境变量
我还尝试通过其安装程序安装tkinter
Windows,安装程序有效,但我仍然遇到相同的问题。我还尝试过更新我的pip
。运行pip list
不会显示tkinter
。
下面是我的main.py文件。
from tkinter import *
from tkinter import filedialog
from tkinter.ttk import Progressbar
import tkinter.font as font
import os.path
from modify_data_pandas import get_column_headings
from modify_data_pandas import scan_file
#Closes window
def close_window():
window.destroy()
#Function checks if the file exists, changes label
def is_valid(*args):
if os.path.isfile(input_file_path.get()):
valid_confirm_lbl.config(text="File Found!")
valid_confirm_lbl.config(fg="green")
return 1
else:
valid_confirm_lbl.config(text="File Not Found")
valid_confirm_lbl.config(fg="red")
return 0
#Sets progress bar value
def setbar(value):
progress_bar['value'] = value
#Gets the current selection in the list box
def add_selection():
if is_valid():
items = headers_listbox.curselection()
already_selected = headers_listbox_selected.get(0, END)
for item in items:
if headers[item] not in already_selected:
headers_listbox_selected.insert(END, headers[item])
#Gets the current selection in the list box
def remove_selection():
if is_valid():
items = headers_listbox_selected.curselection()
pos = 0
for i in items :
idx = int(i) - pos
headers_listbox_selected.delete(idx,idx)
pos = pos + 1
#Add all items in listbox to selection
def add_all():
if is_valid():
items = headers_listbox.get(0, END)
for item in items:
if item not in headers_listbox_selected.get(0, END):
headers_listbox_selected.insert(END, item)
#Remove all items in selection listbox
def remove_all():
if is_valid():
headers_listbox_selected.delete(0, END)
#Scans the file for sensitive data
def scan():
if is_valid():
print("Scan")
change_row, change_col, change_type = scan_file(input_file_path, headers_listbox_selected.get(0, END))
#Anonymizes the file
def anonymize():
if is_valid():
print("do anonymize")
#Opens file dialog to pick a csv file
def choose_file():
input_file_name = filedialog.askopenfilename(initialdir = "/", title = "Select file",filetypes = (("CSV files","*.csv"),))
input_file.delete(0, END)
input_file.insert(END, input_file_name)
#Analyzes the file for sensitive data
def analyze_file():
if is_valid():
headers_listbox.delete(0, END)
headers_listbox_selected.delete(0, END)
global headers
headers = get_column_headings(input_file_path)
#headers = list of column headings from selected file
for item in headers:
headers_listbox.insert(END, item)
#Call function that checks each cell for sensitive data
return 1
else:
return 0
#Configure GUI Window
window = Tk()
window.title("CSV Anonymizer")
window.geometry("413x800")
lbl_frame = Frame(window)
lbl_frame.grid(column=0, row=1)
listbox_lbl_frame = Frame(window)
listbox_lbl_frame.grid(column=0, row=5, pady=5)
listbox_frame = Frame(window)
listbox_frame.grid(column=0, row=6)
listbox_btn_frame = Frame(window)
listbox_btn_frame.grid(column=0, row=7)
listbox_btn_frame_2 = Frame(window)
listbox_btn_frame_2.grid(column=0, row=8)
#Label: "File:"
input_file_prompt = Label(lbl_frame, text="File:",anchor=W, justify=LEFT)
input_file_prompt.pack(side = LEFT)
input_file_prompt.config(width=20)
#Label: "File Not Found / File Found!"
valid_confirm_lbl = Label(lbl_frame, text="", anchor=E, justify=RIGHT)
valid_confirm_lbl.pack(side = RIGHT)
valid_confirm_lbl.config(width=20)
#Create stringvar so file path is verified upon change
input_file_path = StringVar()
#Text Field to display/enter Input File Name
input_file = Entry(window, textvariable=input_file_path, width=10)
input_file.grid(column=0, row=2, padx=20)
input_file.config(width=40)
#Checks if the file is valid upon change
input_file_path.trace_add("write", is_valid)
#Button: "Click to Select a File", opens file browser to select a file
choose_file_btn = Button(window, text = "Click to Select a File", command = choose_file)
select_btn_font = font.Font(family='Helvetica', size=20, weight='bold')
choose_file_btn['font'] = select_btn_font
choose_file_btn.grid(column = 0, row = 0, padx=20, pady=20)
choose_file_btn.config(height=3,width=30)
#Button: "Use This File", starts analyze_file func
use_file_btn = Button(window, text = "Use This File", command = analyze_file)
use_file_btn.grid(column = 0, row = 3, padx=20, pady=(8, 10))
use_file_btn.config(width=13, height=2)
#Label "Select the columns you would like to anonymize"
select_columns_lbl = Label(window, text="Select the columns you would like to anonymize")
bold_font = font.Font(family='Helvetica', size=15, weight='bold')
select_columns_lbl['font'] = bold_font
select_columns_lbl.grid(column=0, row=4)
select_columns_lbl.config(width=40)
#Labels state listbox titles
column_headings_lbl = Label(listbox_lbl_frame, text = "Column Headings")
column_headings_lbl.pack(side=LEFT)
column_headings_lbl.config(width=20)
selected_column_headings_lbl = Label(listbox_lbl_frame, text = "Selected")
selected_column_headings_lbl.pack(side=RIGHT)
selected_column_headings_lbl.config(width=20)
#Left listbox, shows column headings from selected file
headers_listbox = Listbox(listbox_frame, selectmode = MULTIPLE)
headers_listbox.pack(side=LEFT)
headers_listbox.config(width=20)
#Right listbox, shows selected column headings
headers_listbox_selected = Listbox(listbox_frame, selectmode = MULTIPLE)
headers_listbox_selected.pack(side=RIGHT)
headers_listbox_selected.config(width=20)
#Buttons to add and remove current selections from respective listboxes
add_selection_btn = Button(listbox_btn_frame, text = "Add Current Selection", command = add_selection)
add_selection_btn.pack(side=LEFT)
add_selection_btn.config(width=20)
remove_selection_btn = Button(listbox_btn_frame, text = "Remove Current Selection", command = remove_selection)
remove_selection_btn.pack(side=RIGHT)
remove_selection_btn.config(width=20)
#Buttons to add and remove all items from their respective listboxes
add_all_btn = Button(listbox_btn_frame_2, text = "Add All", command = add_all)
add_all_btn.pack(side=LEFT)
add_all_btn.config(width=20)
remove_all_btn = Button(listbox_btn_frame_2, text = "Remove All", command = remove_all)
remove_all_btn.pack(side=RIGHT)
remove_all_btn.config(width=20)
#Label "Select the columns you would like to anonymize"
get_suggestions_lbl = Label(window, text="Would you like to scan the file for sensitive data?")
get_suggestions_lbl['font'] = bold_font
get_suggestions_lbl.grid(column=0, row=9, pady=(15, 10))
get_suggestions_lbl.config(width=40)
#Button: "Scan File", starts scan_file func
get_suggestions_btn = Button(window, text = "Scan File", command = scan)
get_suggestions_btn.grid(column = 0, row = 10, pady=(0, 10))
get_suggestions_btn.config(width=20, height=2)
#Button: "Anonymize", starts anonymize func
anonymize_btn = Button(window, text = "Anonymize", command = anonymize)
anonymize_btn.grid(column = 0, row = 11, pady=(0, 10))
anonymize_btn.config(width=20, height=2)
progress_bar = Progressbar(window, orient = 'horizontal', length = 286, mode = 'determinate')
progress_bar.grid(column = 0, row = 12, pady =2)
progress_bar["maximum"] = 100
progress_bar["value"] = 0
#Button to quit the program
quit_btn = Button(window, text = "Quit", command = close_window)
quit_btn.grid(column = 0, row = 20)
quit_btn.config(width=10, height=2)
window.mainloop()
有什么想法吗?