Sunday 18 December 2022

Use of Entry , .get() , .set(), reading and writing data , entry in python

Now we are learning about .get func ,.set() and entry func
reading/writing files wit python

Entry to take input from user
To use it type

guys this is my youtube channel link 
click here
and here you will get all code on google 
drive click here

from tkinter import *
from tkinter import messagebox as msg
root=Tk()
root.title("Dream")
root.geometry("1500x700+0+0")
v1=Entry(root).pack() this is entry widget


.get function use to get data of entry

def got():
msg.showinfo("you type",f"you type {vv.get()}")
#it get data from
entry field and show in messagebox
vv=StringVar() *this is variable
v1=Entry(root,textvariable=vv).pack()
v2=Button(root,text="show",command=got).pack()
*this is button and we
name it show and add a command to it


.set function use to set data in entry field
def got():
if vv.get()== "open":
vv2.set("you write good") *
if the data from
v1 entry is same to open it show you write good in v2
entry
else:
vv2.set("wrong, we are not happy") *
if the data from
v1 entry is not same to open it show wrong, we are not happy in v2
entry



learn about get in radio and check button, reading and writing a file
use .get() func. with check box

write like this
from tkinter import *
from tkinter import messagebox as msg
root = Tk()
def got():
    if vv4.get() == 1:
        msg.showinfo("success","you oder is taken")
    else:
        msg.showinfo("thanks","thanks for using our servise")

vv4 = IntVar()
root.title("Dream")
root.geometry("1500x700+0+0")
v4 = Checkbutton(root, text="order food",variable=vv4, onvalue=1, offvalue=0,height=2, width=10).pack()
# onvalue=1 mean when checkbox is check the value is 1 else 0    .
.   we use variable to define var in check box
v3 = Button(root, text="show", command=got).pack()
root.mainloop()


use .get() func. with radio box write like this

from tkinter import *
from tkinter import messagebox as msg

root = Tk()


def got():
    if vv4.get() == 1:
        msg.showinfo("thanks","you are a male")
    elif vv4.get() == 2:
        msg.showinfo("thanks","you are a female")
    else:
        msg.showerror("error", "select gender")
vv4 = IntVar()
root.title("Dream")
root.geometry("1500x700+0+0")
Label(root,text="select gender").pack()
v4 = Radiobutton(root,text="male",variable=vv4,value=1).pack()  *value must be different
v5 = Radiobutton(root,text="female",variable=vv4,value=2).pack()
v3 = Button(root, text="show", command=got).pack()

root.mainloop()


use of try and except try: #when try get error it run except except #error name: what you want to run when error comes to read line in python write like this f = open('pac.txt') #the name of file with full extension # read first line data = f.readline() print(data) # Read second line data = f.readline() print(data) # Read third line data = f.readline() print(data) # Read fourth line. and so on.. data = f.readline() print(data) f.close() #output my youtube channel is code with rehaan handling error while reading file def readFile(filename): try: #try will all ways exicute with open(filename, "r") as f: print(f.read()) except FileNotFoundError: # except exicute when it get an error like
"FileNotFoundError","ValueError" etc
        print(f"File {filename} is not found")


readFile("1.txt")
readFile("2.txt")
readFile("3.txt")

 # output is  
thanks
File 2.txt is not found

write into a file

filename = "sample.txt"  #name of file
with open(filename, "w") as f:
    f.write("here the content come whis you want to write")


like 

nam=input("write file name with extwnsion .txt: ")   # first it will take name of file like I type
 code.txt
filename=nam
with open(filename, "w") as f:
     var=input("write message : ") # Then it will take msg witch you want to store in that file
     f.writelines(var)
####### this will I discuss on my youtube channel #########
to be continue(making a login system, etc)







Call bots
a , b , c , d

No comments:

Post a Comment