[英]How can i read csv in Python tkinter at the same time?
I have some questions to you. 我有一些问题要问你。 My first question is I read csv file with import_csv_data function but before I read the data although I have made definition like "global df" it says df is not defined. 我的第一个问题是我使用import_csv_data函数读取了csv文件,但是在读取数据之前,尽管我已经进行了“ global df”之类的定义,但它却未定义df。 If I create a df like df = pd.DataFrame(Columns=["X","Y"]) , this time OptionMenu show X and Y. If i want to show columns of my DataFrame i need to remove df = pd.DataFrame(Columns=["X","Y"]) and then i need to run code again. 如果创建像df = pd.DataFrame(Columns = [“ X”,“ Y”])之类的df,则这次OptionMenu显示X和Y。如果要显示DataFrame的列,则需要删除df = pd。 DataFrame(Columns = [“ X”,“ Y”])然后我需要再次运行代码。 And i have one questiong connected with this question is when i read csv file, to see last df i need to close Window. 我有一个与此问题相关的问题是当我读取csv文件时,要查看上一个df,我需要关闭Window。 So i want to read csv file and i want the program to memorize at the same time. 因此,我想读取csv文件,并且希望该程序同时存储。 The second question is when i changed the SelectedValue on OptionMenu i want to get the SelectedValue -selected column name- and show on tk.Button(Drop Column + SelectedValue) < in short i want to get the column name on OptionMenu and show the name on Button at the same time. 第二个问题是当我在OptionMenu上更改SelectedValue时,我想获取SelectedValue-选定的列名-并在tk.Button(Drop Column + SelectedValue)<上显示同时打开按钮。 Third question is i can not fit button or text or label to Frame. 第三个问题是我无法将按钮或文本或标签放入框架。 It is limitting me i have added a photo that describe that question.[Here is image url. 限制我,我添加了描述该问题的照片。[这里是图像url。 > https://ibb.co/wrkcdfK ] Thank you. > https://ibb.co/wrkcdfK ]谢谢。
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def option_changed(*args):
global SelectedValue
SelectedValue = selection.get()
print(SelectedValue)
def hello():
print("hello!")
def import_csv_data():
global v
global df
global a
csv_file_path = askopenfilename()
print(csv_file_path)
v.set(csv_file_path)
df = pd.read_csv(csv_file_path)
a = csv_file_path
def removenans():
print("Merhaba")
print(df.head())
df = pd.read_csv(csv_file_path)
def plotthefigure():
df.dropna(inplace=True,axis=0)
df.Sex = [1 if i=="female" else 0 for i in df.Sex]
plt.figure(figsize=[6,5])
plt.plot(range(len(df["Sex"])),df["Sex"])
plt.title("Age vs Sex")
# Plot window name
plt.gcf().canvas.set_window_title("Plot Frame")
#Switching Frames
def raise_frame(frame):
frame.tkraise()
# Opening new window
def aboutmenudef():
window = tk.Toplevel(root)
def printdf():
tk.Label(MainFrame,textvariable=df.head()).grid(row=6,
columnspan=7,
sticky='WE',padx=5, pady=5, ipadx=5, ipady=5)
root = tk.Tk()
root.wm_title("Machine Learning Implementer")
# FRAMES #
#
MainFrame = Frame(root)
AboutFrame = Frame(root)
HelpFrame = Frame(root)
#
for frame in (MainFrame,AboutFrame,HelpFrame):
frame.grid(row=0, column=0, sticky=NSEW)
v = tk.StringVar()
entry = tk.Entry(MainFrame,
textvariable=v).grid(row=0,
column=1,
ipadx=100)
head = tk.StringVar()
tk.Label(MainFrame,textvariable=head).grid(row=3, columnspan=2,
sticky='WE',padx=5, pady=5, ipadx=5, ipady=5)
tk.Label(AboutFrame,
text="About Us").grid(row=0,column=0)
tk.Label(MainFrame,text="File Path").grid(row=0,
column=0)
mainlabel = Label(MainFrame,text="DF Columns").grid(row=5,
column=0)
###---------------------- BUTTON ----------------------###
tk.Button(MainFrame,
text=" Read CSV ",
command=removenans).grid(row=1,
column=0)
tk.Button(MainFrame,
text=" Drop Column\n "+SelectedValue,
command=removenans).grid(row=2,
column=0)
tk.Button(MainFrame,
text="Remove NaN Values",
command=removenans).grid(row=3,
column=0)
tk.Button(MainFrame,
text=" Print Head of DF ",
command=printdf).grid(row=4,
column=0)
tk.Button(MainFrame,
text=" Scale Parameters ",
command=printdf).grid(row=10,
column=0)
tk.Button(MainFrame,
text="Plot the Graph",
command=plotthefigure).grid(row=0,
column=2)
###---------------------- CHECKBOX ----------------------###
var = IntVar()
c = Checkbutton(root,text="Independent",variable = var)
c.grid(row=3, column=3)
c.config(state=DISABLED)
###---------------------- MENU & TOOLBARS ----------------------###
menubar = Menu(root)
# FILE MENU #
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Open CSV File",
command=import_csv_data)
#filemenu.add_command(label="Save",
# command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit",
command=_quit)
menubar.add_cascade(label=" File ",
menu=filemenu)
# MENU PLOT SECTION #
plotmenu = Menu(menubar,tearoff=0)
plotmenu.add_command(label="Open Plot",
command=hello)
#plotmenu.add_separator()
plotmenu.add_command(label="Close Program",
command=_quit)
menubar.add_cascade(label=" Plot ",
menu=plotmenu)
# HELP #
helpmenu = Menu(menubar,tearoff=0)
helpmenu.add_command(label="Help Index",
command=hello)
menubar.add_cascade(label=" Help ",
menu=helpmenu)
# ABOUT #
aboutmenu = Menu(menubar,tearoff=0)
aboutmenu.add_command(label="About",
command=lambda:raise_frame(AboutFrame))
menubar.add_cascade(label=" About ",
menu=aboutmenu)
root.config(menu=menubar)
# OPTION MENU #
selection = StringVar(MainFrame)
selection.set("-") # default value
selection.trace("w", option_changed)
w = OptionMenu(MainFrame, selection, *df.columns).grid(row=2,
column=1)
#
root.geometry("800x550")
raise_frame(MainFrame)
root.mainloop()
For the first question i have created a new button named Read Csv, so after i have browsed the csv data i read it with a button. 对于第一个问题,我创建了一个名为“读取Csv”的新按钮,因此在浏览csv数据后,我使用一个按钮对其进行了读取。 For second question i couldnt do this. 对于第二个问题,我无法做到这一点。 But i have found something about label with label.config then you can change the value with a def function that you connected with a button for example. 但是我发现了关于带有label.config的label的信息,然后您可以使用与按钮连接的def函数来更改值。 For the last question, i guess it was about the root.geometry() i have changed it to root.geometry("1200x800") and i have defined it on the top one section below of root = Tk() .... If you have any questions about tkinter you can ask. 对于最后一个问题,我想这是关于root.geometry()的,我已将其更改为root.geometry(“ 1200x800”),并且已在root = Tk()的下面的顶部定义了它。如果您对tkinter有任何疑问,可以询问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.