How I used tkinter button to Import files - python

I would like to run a python program and when clicking a button in a (tkinter) window become import files form any direction and store it to another direction and in database .
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilenames()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='import files', command=UploadAction)
button.grid(row=1,column=1,padx=10,pady=10)
root.mainloop()

Related

How to call another python script from a .py file in a new window

I ran into a problem where I want to click a button on a fullscreen app.
test1
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('500x400')
def cmdopen():
os.system('C:\Users\User\Desktop\test2.py')
btn = Button(text='test', command=cmdopen)
btn.pack()
root.mainloop()
test2
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes("-topmost", True)
btn = Button(text='test2')
btn.pack()
root.mainloop()
What it does it displays the test2 interface, but test one stops responding. What I want is that the test2 will apear above and both will respond and are diffrent windows.
Im bad in english so sorry if I have some problems.
If you're okay with having one "master" window that keeps track of the other windows, then you can do something like this:
from tkinter import *
from tkinter.ttk import *
from functools import partial
class subWindow(Toplevel):
def __init__(self, master=None):
super().__init__(master=master)
def createSubwindow(master):
"""Creates a subWindow of 'master' and sets it's options"""
subWin = subWindow(master)
subWin.title('SubWindow')
subWin.geometry('500x400')
subWin.attributes("-topmost", True)
btn = Button(subWin, text='Button Inside of SubWindow')
btn.pack()
# Creating the master-window
root = Tk()
root.title('MasterWindow')
root.geometry('500x400')
# Creates a partial of the createSubwindow, so that we can execute it easier in the button.
subWinPartial = partial(createSubwindow, root)
# Installs the button, with the partial function as a command.
btn = Button(root, text='Create Sub Window', command=subWinPartial)
btn.pack()
# Runs the mainloop, that handles all the windows.
root.mainloop()

How to get a user-selected file and feed it to a python program in Tkinter

I am currently messing about with Tkinter creating an interface for a project I created. The program takes in a bunch of file paths as inputs for it to run. I'm trying to create a tkinter interface where I can upload the 4 files I need or at least somehow get the filepaths and the. feed those to the program. Here is what I have:
import sys
import os
import comparatorclass
from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile
root=Tk()
root.geometry('1000x1000')
def open_file():
file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')])
if file is not None:
content = file.read()
print(content)
def run_comparator():
comparatorclass.main()
button2 = Button(root, text ='Open', command = lambda:open_file())
button2.pack(side = TOP, pady = 10)
button1 = Button(root,text="hello",command= run_comparator)
button1.pack()
root.mainloop()
as you can see, I have two buttons. The issue I'm having is how to connect my openfile function to my run_comparator function such that the 4 files I need to open are passed on to the run_comparator

Tkinter - Storing folder directory in function and calling it to change directory

I am trying to develop a GUI with tkinter. I have created a function (as a button) to browse for a directory folder ('input folder'). I have a routine linked to another button ('execute') that needs the path from 'input folder'.
I am getting errors when I try to pass the path from 'input folder' into os.chdir inside 'execute'. Example is as follows:
import sys
import os
from tkinter import filedialog
from tkinter import *
window = Tk()
def Input():
filename = filedialog.askdirectory()
global filename
def Extraction():
in_loc = filename
os.chdir(in_loc)
btn = Button(window, text="Extract", bg="black", fg="white", command=Extraction)
btn.pack()
btn2 = Button(text="Input", command=Input).pack()
window.mainloop()
Can anyone reproduce this and tell me what I am doing wrong here?
Thanks :)
Try this:
import sys
import os
from tkinter import filedialog
from tkinter import *
filename = ''
def input_function():
global filename
filename = filedialog.askdirectory()
def extraction():
global filename
in_loc = filename
os.chdir(in_loc)
window = Tk()
btn = Button(window, text="Extract", bg="black", fg="white", command=extraction)
btn.pack()
btn2 = Button(text="Input", command=input_function).pack()
window.mainloop()

tkinter:2 windows pop up at the same time

As a beginner, I am not familiar with tkinter and I don't know how to improve the following codes which are supposed to function like this:
After running the renamer_v.py file, a window will pop up. It displays a short description at the top and beneath it there is an orange colored buttion 'Click Me'. Click the button and then pop up the second window to select a folder. All titles of files in the folder except the hidden ones and subfolders will be given a serialized prefix.
The problem is that the main window and the second widnow pop up at the same time. Howver, the latter is designed to appear after users clicked the button.
Here is my souce code.
renamer_V1.py:
import win32file
import win32con
import tkinter as tk
from tkinter import Button
from clicked import Clicked
root=tk.Tk()
root.geometry("550x200")
label=tk.Label(root,font=("Arial Bold",15),
text='Please select a directory to rename files in the folder:')
label.pack()
c=Clicked()
btn=Button(root,font=("Arial",15),bg='orange',text="Click Me",command=c.clicked)
btn.pack()
c.clicked()
file_lists=os.listdir(c.file_path)
n=0
for file in file_lists.copy():
oldname=c.file_path+os.sep+file
file_flag=win32file.GetFileAttributesW(oldname)
is_hiden=file_flag & win32con.FILE_ATTRIBUTE_HIDDEN
if os.path.isdir(oldname) or is_hiden:
continue
else:
oldname=c.file_path+os.sep+file
newname=c.file_path+os.sep+'('+str(n+1)+')'+file
os.rename(oldname,newname)
n+=1
label=tk.Label(root,text=str(n)+' file(s) renamed.')
label.pack()
root.mainloop()
clicked.py:
from tkinter import filedialog
class Clicked:
file_path=None
def __init__(self):
print()
def clicked(self):
self.file_path=filedialog.askdirectory(title='ReNamer')
The application is completed and now it is working perfectly under MacOS as desired. Here is the code:
import os
from tkinter import filedialog
import tkinter as tk
from tkinter import Button
root=tk.Tk()
class ReName():
def __init__(self):
self.n = 0
self.label1=tk.Label(root)
def rename(self):
file_path = filedialog.askdirectory(title='ReNamer')
self.file_lists = os.listdir(file_path)
for self.file in self.file_lists.copy():
self.oldname = file_path + os.sep + self.file
if os.path.isdir(self.oldname) or self.file.startswith('.'):
continue
else:
self.newname = file_path + os.sep + '(' + str(self.n + 1) + ')' + self.file
os.rename(self.oldname, self.newname)
self.n+=1
self.label1.config(text='{} file(s) renamed'.format(self.n))
self.label1.pack()
btn.config(state='disabled')
ins=ReName()
root.geometry("550x200")
label=tk.Label(root,font=("Arial Bold",15),
text='Please select a directory to rename files in the folder:')
label.pack()
btn=Button(root,font=("Arial",15),bg='orange',text="Click Me",command=ins.rename)
btn.pack()
root.mainloop()

hide hidden folders in tkinter gui

How can I hide hidden folders from appearing when askopenfilename is used i.e only show regular folders (not hidden)
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename(title='Please Select File', filetypes=[('Excel files', ('.xlsx'))])

Categories

Resources