Python Choose File and Read File and Save File Python - learnit

Home Top Ad

Post Top Ad

Friday, August 13, 2021

Python Choose File and Read File and Save File Python

Python Choose File and Read File and Save File Python

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python is simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.


Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program does not catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective


tkinter filedialog

Python Tkinter (and TK) offer a set of dialogs that you can use when working with files. By using these you do not have to design standard dialogs your self. File dialogs help you open, save files or directories. This is the type of dialog you get when you click file, open. This dialog comes out of the module; there is no need to write all the code manually.


Python Tkinter – Text Widget

Tkinter is a graphical user interface (GUI) toolkit used in Python to create user-friendly GUIs. Tkinter is the most widely used and fundamental GUI framework available in Python. Tkinter creates GUIs using an object-oriented approach.


Text Widget

When a user wants to insert multiline text fields, he or she uses the Text Widget. This widget can be used for a variety of applications that require multiline text, such as messaging, sending or displaying information, and many other tasks. In the Textwidget, we can also insert media files such as images and links.


Syntax

T = Text(root, bg, fg, bd, height, width, font, ..)

Optional parameters

root– root window.

bg – background colour

fg – foreground colour

bd – border of widget.

height – height of the widget

width – width of the widget.

font – Font type of the text.

cursor – The type of the cursor to be used

insetofftime – The time in milliseconds for which the cursor blink is off.

insertontime – the time in milliseconds for which the cusrsor blink is on

padx – horizontal padding

pady – vertical padding.

state – defines if the widget will be responsive to mouse or keyboards movements.

highligththickness – defines the thickness of the focus highlight.

insertionwidth – defines the width of insertion character

relief – type of the border which can be SUNKEN, RAISED, GROOVE and RIDGE

yscrollcommand – to make the widget vertically scrollable.

xscrollcommand – to make the widget horizontally scrollable.


Example Code

from tkinter import *
import pygame
from tkinter import filedialog
import time
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
def opentextfile():
    myfile = filedialog.askopenfilename(initialdir="D:/Python/", title="Open Text File",filetypes=(("Text File","*.txt"),))
    textfile = open(myfile,"r")
    readdata=textfile.read()
    mytext.insert(END,readdata)
    textfile.close()
def savetextfile():
    textfile = filedialog.askopenfilename(initialdir="D:/Python/", title="Open Text File",filetypes=(("Text File", "*.txt"),))
    textfile = open(textfile,"w")
    textfile.write(mytext.get(1.0,END))
mytext = Text(root, width=20, height=10,font=("Preah Vihear", 18))
mytext.pack(pady=20)
Openbutton = Button(root,text="Open TextFile",command=opentextfile)
Openbutton.pack(pady=20)
SaveButton = Button(root,text="Save TextFile",command=savetextfile)
SaveButton.pack(pady=20)
root.mainloop()


Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad