File Handling Using Python GUI
In this blog, we will learn how to manage a file using python (Tkinter module).
What is File Handling in Python?
Python supports file handling and allows the user to handle file i.e., to read and write files, along with many other options.
File handling is nothing but a combination of various operations performed on the files such as opening the file, reading the file, and writing the files. Python has a built-in function to open a file.
(For more information on File Handling, kindly refer to this documentationhttps://www.geeksforgeeks.org/file-handling-python/)
Now let us start a project using Python GUI for File Handling is :
Code for the project:
For the code of this project click on the link below:
Define code working:-
from tkinter import *
Tkinter is the name of the GUI library in Python. Creating a GUI application using Tkinter is an easy task, so we used it in our project Tkinter Module.
root = Tk()
root.title(“File Handling”)
root.geometry(“500x300”)
Tk class is used to create a root window. Tkinter provides many methods, one of them is the geometry() method. This method is used to set the dimensions of the Tkinter window and it is used to set the position of the main window on the user desktop.
def open_file():
global text1
text1.set(“”)
if entry_window.get():
file = open(entry_window.get(),’r’)
for each in file:
text1.set(each)
Now come to the next line of the code where we are going to work on the function part of the GUI project, here we define a function, the function name is open_file.In this line of the code, we define the global variable. The name of the global variable is text1. In our previous Python GUI Project we already better understand how global variables work. Now in this line of code, we use Set Function. Here Set Function is used to assign the new value of the variable so here we apply Set Function on the text1 variable with a null value.
def create_file():
global text1
text1.set(“”)
if entry_window.get():
f = open(entry_window.get(), “x”)
text1.set(“Create File Successfully”)
Now come to the next line of the code, here we again define the create_file function. Here in this line of the code, we define the global variable text1. Now again we work on the Set Function, here we define the set function on the text1 variable with the null value. Here in this line we again define the if condition where we get the data from the entry_window object. Now here we define the f variable for holding the open new file which does not exist in any directory. Here we again Set the text1 variable value with “Create File Successfully”.
def append_text():
global text2
text1.set(“”)
if entry_window.get():
file = open(entry_window.get(),’r’)
for each in file:
text1.set(each)
file3=open(entry_window.get(),”a”)
if entry_window3.get():
file3.write(entry_window3.get())
file3.close()
text1.set(“Append Successfully”)
Now in this line of the code, we again define the append_text function. The global variable name is the text2 variable. Again we work with the Set Function, the value of the text1 variable is null. Now again we use the if condition in our code, here we define the if condition where we get the value from the entry_window object. Here we define the file object where the file object holds the open file in the read mode.
Now in this line of the code, we define the for loop to read all strings in the file object. Here we again use Set Function and set the text1 variable value from the for loop collected data. Now in this line of the code, we define the file3 object to hold the open file in append mode.
Now in this line of the code, we define the if condition where we get the data from the entry_window3 object. In this line of the code, we perform the write operation on the file3 object. Now again use the Set Function on the text1 variable is: “Append Successfully”.
text1 = StringVar()
text2 = StringVar()
entry_window = Entry(root,justify=CENTER, width=40, borderwidth=4, textvariable=text)
entry_window.place(x=120,y=40)
Now in this line of the code, we define the text1 variable with the type of variable is StringVar type. Now come to the Designing Part of the GUI window. Here we are using some Label, Entry Box and Button in a project. So let’s start to understand the working of Label, EntryBox and Button in a project according to the project code line by line. Now in this line of the code, we define the Entry Box.
btn_check = Button(root,text=”Open File”, command=open_file)
btn_check.place(x=210,y=80)
entry_window2 = Entry(root,justify=CENTER, width=40,borderwidth=4, textvariable=text1)
entry_window2.place(x=120,y=120, height=50)
label = Label(root,text=”Enter text for append:”)
label.place(x=120,y=175)
entry_window3 = Entry(root,justify=CENTER, width=40, borderwidth=4, textvariable=text2)
entry_window3.place(x=120,y=200)
Now come to the Button part of the project in this Button Method we are performing an action when we clicked on the Button. So let’s start with how Button works in the next line of the code of the program. Here we define a btn_check object of the Button Method on the GUI window and give the find command on the Button for performing an action when Button clicks. What type of action performs Button it all depends on the instruction or a condition giving on the Button function.
Now in this line of code, we have created button’s, entry and label.
btn_check1 = Button(root,text=”Create File”, command=create_file)
btn_check1.place(x=80,y=240)
btn_check2 = Button(root,text=”Append Text”, command=append_text)
btn_check2.place(x=210,y=240)
btn_check3 = Button(root,text=”Close File”, command=root.destroy)
btn_check3.place(x=350,y=240)
root.mainloop()
Now in this line of code, we have created button’s, entry and label. Now come to the last line of code of the project is the main loop function that provides an infinite loop. So let’s start to understand how it works. Here the main loop() method is an infinite loop used to run the application, using this function window is not closed as long as.
(For more information on File Handling, kindly refer to this documentationhttps://www.geeksforgeeks.org/file-handling-python/)
Here we complete a full GUI Project using the Python Tkinter module and File Handling Methods.
Refer to our more Blogs on Python from the link below:
https://medium.com/@platforuma
Visit Our Website:
Author: Priyanka Prasad
Editor: Riya Patidar, Devendra Patidar, Ashutosh Raghuwanshi