Home > other >  Tkinter: how to get the path of the file or folder, and show the Label?
Tkinter: how to get the path of the file or folder, and show the Label?

Time:09-16

Have a program, need to click the button to select a file or folder, select, also can display path behind the button,

The code is as follows:
 import tkinter as tk 
The import tkinter. Filedialog as fd
The import plotly as py
The import plotly. Graph_objs as go
The import openpyxl
The import pandas as pd
The import OS


The class App (tk. Tk) :

Def __init__ (self) :
Super () __init__ ()
The self. The filename='
The self. The directory='
Group_1=tk. LabelFrame (self, padx=15, pady=10,
Text="Input and Output Settings")
Group_1. Pack (padx=10, pady=5)

Tk. The Button (group_1, text="Step1: Import data from",
Highlightbackground='green',
Width=20, the command=self choose_file). The grid (row=0, the column=0)
Self. Label1=tk. Label (group_1, text=self. The filename, width=20). The grid (row=0, the column=1)
Tk. The Button (group_1, text="Step2: Set the output path", highlightbackground='orange',
Width=20, the command=self choose_directory). The grid (row=1, the column=0)
Tk. Label (group_1, text=self. Directory, width=20). The grid (row=1, the column=1)

Group_2=tk. LabelFrame (self, padx=15, pady=10, text="Implementation")
Group_2. Pack (padx=10, pady=5)
Tk. The Button (group_2, text="Step3: Start to plot," highlightbackground='blue',
Width=20, the command=self. The start). The grid (row=0)


Def choose_file (self) :
Filetypes=((" Excel files ", "*. XLSX"),
)
The self. The filename=fd. Askopenfilename (title="Open file",
Initialdir="/", filetypes=filetypes)
If the self. The filename:
Print (self. Filename)

Def choose_directory (self) :
Self. Directory=fd. Askdirectory (title="of" the Open directory,
Initialdir="/")
If the self. The directory:
Print (self. Directory)

Def start (self) :
Self. SP (self. The filename, the self. The directory)


Def SP (self, input_file output_dir) :
The self. The input_file=input_file
Self. Output_dir=output_dir
Wb=openpyxl. Load_workbook (self. Input_file)
Sheet=wb/' Sheet1 '
Row_max=sheet. Max_row
Col_max=sheet. Max_column
First_row_list=[]
First_col_list=[]
, col_max for col_n in range (2 + 1) :
First_row_list. Append (sheet) cell (row=1, the column=col_n) value)
, row_max for row_n in range (2 + 1) :
First_col_list. Append (sheet) cell (row=row_n, column=1). The value)

Data_all=pd. Read_excel (self. Input_file)
Data_selected=data_all. Loc [: first_row_list]

Df=pd DataFrame (data_selected)
Df. Index=first_col_list
Colors=[' RGB (150204, living) ', 'RGB (255, 130, 71)', 'RGB (255, 193, 37)', 'RGB (180240190)', 'RGB (255, 10, 1),
'RGB (25, 19, 3)', 'RGB (100, 100, 100)', 'RGB (45,24,200)', 'RGB (33, 58, 108)', 'RGB (35, 208, 232)]

Data=https://bbs.csdn.net/topics/[go. Scatter (
X=df columns,
Y=[country] * len (df) columns),
Mode='markers + text,
Marker=dict (
Color=colors (num),
Size=df. Loc [country],
Showscale=False,
),
Text=list (map (STR, df. Loc [country])),
Textposition='middle center',
)
For num, the country in enumerate (reversed (df) index))
]

Layout=go. Layout (plot_bgcolor='RGB (10, 10, 10),
Paper_bgcolor='RGB (20, 55, 100),
The font={
'the size: 15,
'family' : 'sans-serif,
'color' : 'RGB (255, 255, 255)'
},
Width=1000,
Height=800,
Xaxis=dict (
Title='Output of grapes per year in the company countries',
Nticks=col_max + 1,
Type='category',
),
Showlegend=False,
Margin=dict (l=100 r=100, t=100, b=100),
Hovermode=False,
)

FIG.=go Figure (data=https://bbs.csdn.net/topics/data, layout=layout)
Py. Offline. The plot (FIG, filename=OS. Path. Join (self) output_dir, 'bubble_diagram. HTML))


If __name__=="__main__" :
App=app ()
App. The title (" mercifully Diagram ")
App. Mainloop ()

After the operation, the UI select files and folders, no instant shows path, how to modify?
  • Related