Python Global Keyword - learnit

Home Top Ad

Post Top Ad

Friday, May 14, 2021

Python Global Keyword

Python Global Keyword and Create Fucntion Pause

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 doesn't 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.


Python Global Keyword and Create Fucntion Pause

In this article, you will learn about the global keyword, global variable and when to use global keywords.

What is the global keyword?


In Python Global, keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

Rules of global Keyword

The basic rules for Global keyword in Python are:

-When we create a variable inside a function, it is local by default.

-When we define a variable outside of a function, it is global by default. You don't have to Use Global keyword.

-We use Global keyword to read and write a global variable inside a function.

-Use of Global keyword outside a function has no effect


Example:

global paused
paused = False

Python Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Creating a Function

In Python a function is defined using the def keyword:

def pause(is_paused):
global paused
paused = is_paused


Example

from tkinter import*
import pygame
from tkinter import filedialog
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()

def addsongmusic():
song = filedialog.askopenfilename(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
song = song.replace("D:/Python/Test2/sound/","")
song = song.replace(".mp3", "")
songbox.insert(END,song)

def addsongsmusic():
songs = filedialog.askopenfilenames(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
for song in songs:
song = song.replace("D:/Python/Test2/sound/", "")
song = song.replace(".mp3", "")
songbox.insert(END, song)

def playmusic():
song = songbox.get(ACTIVE)
song = f'D:/Python/Test2/sound/{song}.mp3'
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)

def stopmusic():
pygame.mixer.music.stop()
songbox.select_clear(ACTIVE)
global paused
paused = False
def pause(is_paused):
global paused
paused = is_paused
if paused:
pygame.mixer.music.unpause()
paused = False
else:
pygame.mixer.music.pause()
paused = True

songbox= Listbox(root, bg="black", fg="green", width=80,selectbackground="gray",selectforeground="black")
songbox.pack(pady=20)
backbtn= PhotoImage(file='image/forward101.png')
forwardbtn= PhotoImage(file='image/forward101.png')
playbtn= PhotoImage(file='image/Play101.png')
pausebtn= PhotoImage(file='image/pause101.png')
stopbtn= PhotoImage(file='image/Stop101.png')
controlframe= Frame(root)
controlframe.pack()
backbutton= Button(controlframe, image=backbtn, borderwidth=0)
frowardbutton= Button(controlframe, image=forwardbtn, borderwidth=0)
playbutton= Button(controlframe, image=playbtn, borderwidth=0, command=playmusic)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0, command=lambda: pause(paused))
stopbutton= Button(controlframe, image=stopbtn, borderwidth=0, command=stopmusic)

backbutton.grid(row=0, column=1, padx=10)
frowardbutton.grid(row=0, column=2, padx=20)
playbutton.grid(row=0, column=3, padx=20)
pausebutton.grid(row=0, column=4, padx=20)
stopbutton.grid(row=0, column=5, padx=20)
#create Menu mymenu = Menu(root)
root.config(menu=mymenu)
addsong = Menu(mymenu)
mymenu.add_cascade(label="file", menu=addsong)
addsong.add_command(label="Open Song or Playlist",command=addsongmusic)
addsong.add_command(label="Open Many Songs or Playlist",command=addsongsmusic)
root.mainloop()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad