pass tkinter openfilename to a instance variable - python

Checked:
python pass variable tkinter
Tkinter - How to pass instance variable to another class?
and other web resources but didn't find a solution.
I have a Tkinter button to open file:
import tkinter as tk
from tkinter import filedialog as fd
from myClass import my_function
class select_file:
def __init__(self):
self.root = tk.Tk()
self.filename = ""
tk.Button(self.root, text="Browse", command=self.open).pack()
self.root.mainloop()
def open(self):
filetypes = (('LOG files', '*.LOG'), ('All files', '*.*'))
self.filename = fd.askopenfilename(title='open LOG file', filetypes=filetypes)
self.root.destroy()
print(self.filename)
return self.filename
and that's work fine since I can get the file path in right way.
Output:
C:/Users/xxxx/xxxxx/Dataset/SC180918.LOG
My question is how I can pass the selected filename as instance variable to other function within same project but in another file (myClass.py):
class my_function:
log_frame = pd.DataFrame()
def __init__(self, file):
self.file = file
def create_df(self):
myClass.log_frame = pd.read_table(self.file, sep=';', index_col='TimeStamp', parse_dates=True)
return myClass.log_frame
My task is to pass the file to create_df.
I tried several code I will not post to avoid confusion and not working code.

As the file is important, I generated two python files as below:
myClass.py:
class my_function:
def __init__(self, file):
self.file = file
def create_df(self):
print("filename in my_function is ",self.file)
testMe.py:
import tkinter as tk
from tkinter import filedialog as fd
from myClass import my_function
class select_file:
def __init__(self):
self.root = tk.Tk()
self.filename = ""
tk.Button(self.root, text="Browse", command=self.open).pack()
self.root.mainloop()
def open(self):
filetypes = (('LOG files', '*.LOG'), ('All files', '*.*'))
self.filename = fd.askopenfilename(title='open LOG file', filetypes=filetypes)
self.root.destroy()
print("filename in select_file is ",self.filename)
return self.filename
sfClass = select_file() # select filename by Browse
filename = sfClass.filename # get filename from the class
mfClass = my_function(filename) # pass it to the class
mfClass.create_df()
here is the output if I select testMe.py file:
filename in select_file is C:/Users/XXX/XXX/SOquestions/testMe.py
filename in my_function is C:/Users/XXX/XXX/SOquestions/testMe.py
as a result, filename has been successfully passed to the my_function.
I tested as below as well:
print(mfClass.file)
it prints: C:/Users/XXX/XXX/SOquestions/testMe.py

I would do as following;
sfClass = select_file() # select filename by Browse
filename = sfClass.filename # get filename from the class
mfClass = my_function(filename) # pass it to the class

Related

Cant get code to execute in thread with Tkinter GUI

Here is the code it is supposed to scan and enumerate a directory and make hashes of whatever files it finds and append them to a dict the code all works separately but I cant get the hashing part to trigger within the threaded code there is no error
import threading
import tkinter as tk
import tkinter.ttk as ttk
import hashlib
import glob
class global_variables:
def __init__(self):
pass
DirSelected = ''
Manifest = []
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Progress bar example")
self.button = tk.Button(self, text="Start", command=self.start_action)
self.button.pack(padx=10, pady=10)
def start_action(self):
self.button.config(state=tk.DISABLED)
t = threading.Thread(target=self.contar)
t.start()
self.windows_bar = WinProgressBar(self)
def contar(self):
directories_scanned = [str, 'File,Md5!'] # , separator value ! delineator adds new line
target_dir = '/Users/kyle/PycharmProjects/' # this wont run
files = glob.glob(target_dir + '/**/*.*', recursive=True) # Line adds search criteria aka find all all to the
# directory for GLOB
for file in files:
with open(file, "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
directories_scanned.append(
file + ',' + str(file_hash.hexdigest() + '!')) # Appends the file path and Md5
global_variables.Manifest = directories_scanned
for x in range(len(global_variables.DirSelected)): # testing to view files TODO REMOVE this
print(global_variables.DirSelected[x])
for i in range(10): #to here
print("Is continuing", i)
print('stop')
self.windows_bar.progressbar.stop()
self.windows_bar.destroy()
self.button.config(state=tk.NORMAL)
class WinProgressBar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.title("Progress")
self.geometry("300x200")
self.progressbar = ttk.Progressbar(self, mode="indeterminate")
self.progressbar.place(x=30, y=60, width=200)
self.progressbar.start(20)
if __name__ == "__main__":
global_variables()
app = App()
app.mainloop()
print("Code after loop")
for x in range(len(global_variables.DirSelected)): # testing to view files TODO REMOVE this
print(global_variables.DirSelected[x])
No errors can be shown this time the code will run
It works fine for me. I think you just were checking the wrong variable DirSelected which was in fact an empty string. (see my code)
Also, I moved your WinProgressBar so it is available when the thread is executing. (was getting an exception on this).
import threading
import tkinter as tk
import tkinter.ttk as ttk
import hashlib
import glob
class global_variables:
def __init__(self):
pass
DirSelected = ''
Manifest = []
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Progress bar example")
self.button = tk.Button(self, text="Start", command=self.start_action)
self.button.pack(padx=10, pady=10)
def start_action(self):
self.button.config(state=tk.DISABLED)
self.windows_bar = WinProgressBar(self)
t = threading.Thread(target=self.contar)
t.start()
def contar(self):
directories_scanned = [str, 'File,Md5!'] # , separator value ! delineator adds new line
target_dir = '/home/jsantos/workspace/blog.santos.cloud/' # this wont run
files = glob.glob(target_dir + '/**/*.*', recursive=True) # Line adds search criteria aka find all all to the
# directory for GLOB
for file in files:
with open(file, "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
directories_scanned.append(
file + ',' + str(file_hash.hexdigest() + '!')) # Appends the file path and Md5
global_variables.Manifest = directories_scanned
for x in range(len(global_variables.Manifest)): # testing to view files TODO REMOVE this
print(global_variables.Manifest[x])
for i in range(10): #to here
print("Is continuing", i)
print('stop')
self.windows_bar.progressbar.stop()
self.windows_bar.destroy()
self.button.config(state=tk.NORMAL)
class WinProgressBar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.title("Progress")
self.geometry("300x200")
self.progressbar = ttk.Progressbar(self, mode="indeterminate")
self.progressbar.place(x=30, y=60, width=200)
self.progressbar.start(20)
if __name__ == "__main__":
global_variables()
app = App()
app.mainloop()
print("Code after loop")
for x in range(len(global_variables.Manifest)): # testing to view files TODO REMOVE this
print(global_variables.Manifest[x])

TypeError: open_file() missing 1 required positional argument: 'self'

I'm creating a notepad as my first GUI project..
I create a toolbar with file and edit cascade menu, with some commands in them.
But when I try to open a file or save a file, it gives me this error TypeError: open_file() missing 1 required positional argument: 'self' . But when I use shortcuts like CTRL + O , it works..
Part of my code:
import os,sys
from tkinter import *
import tkinter.messagebox,tkinter.filedialog
class Commands:
def open_file(self):
self.my_filetypes = [('all files', '.*'), ('text files', '.txt')]
self.askPath = tkinter.filedialog.askopenfile(parent=root,
initialdir=os.getcwd(),
title="Open file",
filetypes=self.my_filetypes,
mode="r")
self.path_f = self.askPath.name
with open(self.path_f,"r") as file:
content = file.read()
notepad.delete("1.0", "end")
notepad.insert(END,content)
class Window:
def framesForDays(self):
#Toolbar frame
self.toolbarFrame = Frame(root,relief="groove")
self.toolbarFrame.pack(side="top",fill="both")
#Text frame
self.textFrame = Frame(root,relief="groove")
self.textFrame.pack(side="top",fill="both",expand=True)
def settings(self):
self.m = Menu(self.toolbarFrame)
root.title("Notepad")
root.config(menu=self.m)
def subMenu(self):
self.submenu=Menu(self.m)
self.m.add_cascade(label='File',menu=self.submenu)
self.submenu.add_command(label='Open', accelerator="CRTL+O",command=(Commands.open_file))
root.bind_all("<Control-O>", (Commands.open_file)) and root.bind_all("<Control-o>", (Commands.open_file))
def notePad(self):
global notepad
notepad = Text(self.textFrame)
notepad.pack(side="left",expand=True,fill="both")
#Scroll bar
scroll_y = Scrollbar(self.textFrame, orient="vertical", command=notepad.yview)
scroll_y.pack(side="left", expand=False, fill="y")
notepad.configure(yscrollcommand=scroll_y.set)
root = Tk()
wnd = Window()
wnd.framesForDays()
wnd.settings()
wnd.subMenu()
wnd.notePad()
root.mainloop()
Since you define open_file with a self parameter.It requires an instance, called like:
my_object=Commands()
my_object.open_file()
Commands.open_file(my_object)
But in your case,self is not necessary.You can define it as a static method
class Commands:
#staticmethod
def open_file():
...
Then you can call it without any instance,like:
Commands.open_file()

Use Tkinter to Open file, run script and export file

I would like to create a pyton script that takes an excel file (provided by the user), run a script, and save this excel file (with a file name provided by the user).
I would like to implement some buttons using Tkinter to enable non-programmers to easily use my algorithm.
I tried to create a ''load data'' and ''display data'' button (I took a code online) that both work.
However I didn't manage to run the script on the database imported (as I don't know how to access it)
I didnt manage to create a button that enables the user to choose the name of the file they want to save it to.
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
import pandas as pd
import csv
# --- classes ---
class MyWindow:
def __init__(self, parent):
self.parent = parent
self.filename = None
self.df = None
self.text = tk.Text(self.parent)
self.text.pack()
self.button = tk.Button(self.parent, text='LOAD DATA', command=self.load)
self.button.pack()
self.button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.button.pack()
self.button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.button.pack()
def load(self):
name = askopenfilename(filetypes=[('CSV', '*.csv',), ('Excel', ('*.xls', '*.xslm', '*.xlsx'))])
if name:
if name.endswith('.csv'):
self.df = pd.read_csv(name)
else:
self.df = pd.read_excel(name)
self.filename = name
display directly
self.text.insert('end', str(self.df.head()) + '\n')
def display(self):
# ask for file if not loaded yet
if self.df is None:
self.load_file()
# display if loaded
if self.df is not None:
self.text.insert('end', self.filename + '\n')
self.text.insert('end', str(self.df.head()) + '\n')
#def script_python(self):
# self.df=self.df.iloc[:, :-1]
#this is not my original script
#def file_save(self):
# savefile = asksaveasfilename(filetypes=(("Excel files", "*.xlsx"),
# ("All files", "*.*") ))
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
top = MyWindow(root)
root.mainloop()
My python script that do analysis on the excel file works, so I just put a very simple python script to make your life easier.
Thank you
I'm new at Tkinter and not used to classes and dictionaries. thanks for your help
I've modified your code, adding a button to run your script and a button to save the data (I've tried to keep the rest as close to your original code as possible):
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
import pandas as pd
import csv
# --- classes ---
class MyWindow:
def __init__(self, parent):
self.parent = parent
self.filename = None
self.df = None
self.text = tk.Text(self.parent)
self.text.pack()
# load data button
self.load_button = tk.Button(self.parent, text='LOAD DATA', command=self.load)
self.load_button.pack()
# display data button
self.display_button = tk.Button(self.parent, text='DISPLAY DATA', command=self.display)
self.display_button.pack()
# run script button
self.script_button = tk.Button(self.parent, text='RUN SCRIPT', command=self.script_python)
self.script_button.pack()
# save data button
self.save_button = tk.Button(self.parent, text='SAVE DATA', command=self.file_save)
self.save_button.pack()
def load(self):
name = askopenfilename(filetypes=[('CSV', '*.csv',), ('Excel', ('*.xls', '*.xslm', '*.xlsx'))])
if name:
if name.endswith('.csv'):
self.df = pd.read_csv(name)
else:
self.df = pd.read_excel(name)
self.filename = name
# display directly
self.text.insert('end', str(self.df.head()) + '\n')
def display(self):
# ask for file if not loaded yet
if self.df is None:
self.load_file()
# display if loaded
if self.df is not None:
self.text.insert('end', self.filename + '\n')
self.text.insert('end', str(self.df.head()) + '\n')
def script_python(self):
# replace this with the real thing
self.df = self.df.iloc[:, :-1]
def file_save(self):
fname = asksaveasfilename(filetypes=(("Excel files", "*.xlsx"),
("All files", "*.*")))
# note: this will fail unless user ends the fname with ".xlsx"
self.df.to_excel(fname)
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
top = MyWindow(root)
root.mainloop()

Python tkinter passing input value within the same class

Most of the code below is just so the problem is accurately replicated, the issue most likely is in the hand-off of filename fromaskopenfilenameI() to printing(stringToPrint)or the if statement at the end.
Goal
The goal of this program is to simply print the file path to the console when Print File Path button is clicked.
Current state
When the program is executed the window appears correctly and the it lets you select a file in the file system. Once the file is opened, the script seems to call printing method before it executes its own print statement. When I open another file it prints the only the print statement from askopenfilename() (which is correct).
However, clicking the Print File Path Button does not seem to get work at any stage.
An example output would be:
Value on click: no-file
askopenfilename value: C:/Temp/AFTER_FIRST_OPEN.txt
askopenfilename value: C:/Temp/AFTER_SECOND_OPEN.txt
Code
import Tkinter, Tkconstants, tkFileDialog
filename = 'no-file'
class TkApp(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
# options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
# define buttons
Tkinter.Button(self, text='Open File', command=self.askopenfilename).pack(**button_opt)
Tkinter.Button(self, text='Print File Path', command=self.printing(filename)).pack(**button_opt)
Tkinter.Button(self, text='Quit', command=self.quit).pack(**button_opt)
# define options for opening or saving a file
self.file_opt = options = {}
options['defaultextension'] = '.twb'
options['filetypes'] = [('All', '*')]
options['initialdir'] = 'C:\\'
options['parent'] = root
options['title'] = 'Select a File'
def askopenfilename(self):
"""Returns an opened file in read mode.
This time the dialog just returns a filename and the file is opened by your own code.
"""
global filename
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
print "askopenfilename value: " + filename
return filename
def printing(self, stringToPrint):
print "Value on click: " + stringToPrint
def quit(self):
root.destroy()
if __name__=='__main__':
root = Tkinter.Tk()
root.title("Path Printer")
TkApp(root).pack()
root.mainloop()
The root of your problem is that you're calling self.printing at the time you create the button. The solution is simple: change self.printing(filename) to be just self.printing, and then have self.printing print an attribute that is set by self.askopenfilename.
For example:
class TkApp(Tkinter.Frame):
def __init__(self, root):
...
self.currentFile = None
...
Tkinter.Button(self, text='Open File', command=self.askopenfilename)
Tkinter.Button(self, text='Print File Path', command=self.printing)
...
def askopenfilename(self):
...
self.currentFile = tkFileDialog.askopenfilename(**self.file_opt)
...
def printing(self):
print "Value on click: " + self.currentFile
Note: a return statement in a function that is called from a button serves absolutely no purpose. The actual caller of the button ignores all return values.
I think the problem is that the self.printing function cannot take arguments when called from a button press. This is common in many GUI libraries. To get around this, I would recommend that you change filename to self.filename, so that it can be called from self.printing even if it isn't passed.
The code would be:
import Tkinter, Tkconstants, tkFileDialog
class TkApp(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
# options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
#moved initiation of filename to here so it will print if no file has been selected
self.filename = 'no-file'
# define buttons
Tkinter.Button(self, text='Open File', command=self.askopenfilename).pack(**button_opt)
Tkinter.Button(self, text='Print File Path', command=self.printing).pack(**button_opt) #not the removal of variable.
Tkinter.Button(self, text='Quit', command=self.quit).pack(**button_opt)
# define options for opening or saving a file
self.file_opt = options = {}
options['defaultextension'] = '.twb'
options['filetypes'] = [('All', '*')]
options['initialdir'] = 'C:\\'
options['parent'] = root
options['title'] = 'Select a File'
def askopenfilename(self):
"""Returns an opened file in read mode.
This time the dialog just returns a filename and the file is opened by your own code.
"""
# get filename - edited to be part of self
self.filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if self.filename:
print "askopenfilename value: " + self.filename
return self.filename
def printing(self):
print "Value on click: " + self.filename
def quit(self):
root.destroy()
if __name__=='__main__':
root = Tkinter.Tk()
root.title("Path Printer")
TkApp(root).pack()
root.mainloop()

python 2.7 Tk change label to carry file name from opened file

I have this code in python 2.7 using tkinter that creates a Button on a Frame to open a file. There's a Label under it. I'm trying to make it so once the file is opened, the label prints the path, "file1.name" or whatever, on the Label, and if you open a new file it will change that Label again.
Also, I'm betting there's a better way to move data between the functions than I'm using here with global, but that's not worrying me right now.
I have to move the data from the opened files between functions so that I can mix the data and save to a new file. The code is:
from Tkinter import *
import Tkinter
import tkFileDialog
import tkMessageBox
root = Tkinter.Tk()
global rfile1
global rfile2
rfile1 = ""
rfile2 = ""
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid()
self.createWidgets1()
self.createLabels1()
self.createWidgets2()
self.createLabels2()
def createWidgets1(self):
self.oButton = Button(self, text = "open1", command = self.openfile1)
self.oButton.grid()
def createLabels1(self):
self.oLabel = Label(self, text = "whoops")
self.oLabel.grid()
def createWidgets2(self):
self.oButton = Button(self, text = "open2", command= self.openfile2)
self.oButton.grid()
def createLabels2(self):
self.oLabel2 = Label(self, text = "whoops2")
self.oLabel2.grid()
def openfile1(self):
file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb')
rfile1 = file1.read()
tkMessageBox.showinfo("oy", rfile1, parent=root)
def openfile2(self):
file2 = tkFileDialog.askopenfile(parent=root, mode='rb')
rfile2 = file2.read()
tkMessageBox.showinfo("hola", rfile2, parent=root)
app = Application()
app.master.title("whiggy whompus")
app.mainloop()
If I understand correctly, you want something like (untested):
def openfile1(self):
file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb')
self.oLabel.configure(text=file1.name)
rfile1 = file1.read()
tkMessageBox.showinfo("oy", rfile1, parent=root)
#mgilson has solved your first question. Your second question, about how to pass parameters between functions without using globals :
you might want to look at storing the variables as attributes on your application class :
The syntax self. is an attribute on the current instance (an instance being a particular example of a class - just like your car is a specific example of a class "car").
You can use instance attributes in this example as if they are globals.
from Tkinter import *
import Tkinter
import tkFileDialog
import tkMessageBox
root = Tkinter.Tk()
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid()
self.createWidgets1()
self.createLabels1()
self.createWidgets2()
self.createLabels2()
self.rfile1 = ""
self.rfile2 = ""
def createWidgets1(self):
self.oButton = Button(self, text = "open1", command = self.openfile1)
self.oButton.grid()
def createLabels1(self):
self.oLabel = Label(self, text = "whoops")
self.oLabel.grid()
def createWidgets2(self):
self.oButton = Button(self, text = "open2", command= self.openfile2)
self.oButton.grid()
def createLabels2(self):
self.oLabel2 = Label(self, text = "whoops2")
self.oLabel2.grid()
def openfile1(self):
file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb')
self.rfile1 = file1.read()
tkMessageBox.showinfo("oy", self.rfile1, parent=root)
def openfile2(self):
file2 = tkFileDialog.askopenfile(parent=root, mode='rb')
self.rfile2 = file2.read()
tkMessageBox.showinfo("hola", self.rfile2, parent=root)
app = Application()
app.master.title("whiggy whompus")
app.mainloop()

Categories

Resources