This may be tricky but I’ll try to put it clear. i have created two buttons, 1st button search folder and select individual filename as input, 2nd button select folder and take its directory as input and read all its files inside.
I want to create 3rd button(Execute) where it should know which button i selected and based on that it needs to do execute function.
Now,
JavaScript
x
61
61
1
import cx_oracle
2
conn = cx_oracle.connect()
3
4
from tkinter import*
5
from tinter import filedialog as fd
6
import pandas as pd
7
from tkinter import messagebox
8
from tkinter.messagebox import showinfo
9
10
window=Tk()
11
window.withdraw
12
window.title("ETL Automation")
13
winow.geometry("400x250+10+10")
14
window['bacground']='#34495E'
15
16
cursor = conn.cursor
17
global filename
18
19
def select_folder()
20
folder_selected = filedialog.askdirectory()
21
22
def select_individual_file()
23
filetypes = (
24
'text files','*sql'),
25
('All files', '*.*')
26
)
27
global filename
28
filename = fd.askopenfilename(title= ' ',
29
initiadir=r'C:UsersDocuments',
30
filetypes=filetypes)
31
32
#Now i wrote this for to execute 1 sql file
33
def execute_query
34
with open(filename) as inserts:
35
query = inserts.read()
36
cursor.execute(query)
37
global row
38
row = cursor.fetchall()
39
messagebox.showinfo('Alert',"Query executed")
40
41
42
#This code to execute the sql files inside the folder_selected
43
cd = (folder_selected)
44
45
for root, dirnames, filenames in os.walk(folder_selected)
46
for filenamess in fnmatch.filter(filnames, '*sql'):
47
with open (cd+'\'+filenamess) as inserts:
48
queries = inserts.read()
49
cursor.execute(queries)
50
rows = cursor.fetchall()
51
52
53
btn = Button(window, text="Select in Batch", fg = 'black',command=folder_selected,font="10")
54
btn = Button(window, text="Select single file", fg = 'black',command=select_individual_file,font="10")
55
btn = Button(window, text="Execute", fg = 'black',command=execute_query,font="10")
56
57
btn.place(x=40, y=80)
58
btn1.place(x=40,y=80)
59
btn1.place(x=40,y=80)
60
window.mainloop
61
My intention is to keep the same button to execute button for both the folder_selected and select_individual_file, but the execute button have to identify which input to take by writing the condition or something. Please help.
Advertisement
Answer
You can simply use single global variable and use os.path.isdir()
and os.path.isfile()
to check whether a folder or a file is selected:
JavaScript
1
29
29
1
2
import os
3
from tkinter import filedialog as fd
4
5
6
selected_path = '' # initialise the global variable
7
8
def select_folder():
9
global selected_path
10
selected_path = fd.askdirectory()
11
12
def select_individual_file():
13
global selected_path
14
filetypes = (
15
('SQL files', '*.sql'),
16
('All files', '*.*'),
17
)
18
selected_path = fd.askopenfilename(initialdir='C:/Users/Documents', filetypes=filetypes)
19
20
def execute_query():
21
if os.path.isdir(selected_path):
22
print('Folder selected:', selected_path)
23
elif os.path.isfile(selected_path):
24
print('File selected:', selected_path)
25
else:
26
print('No item selected')
27
28
29