Monday, January 29, 2024

Python programs dictionary functions


Python programs dictionary functions


 # Create a dictionary

student = {'name': 'John', 'age': 25, 'grade': 'A'}


# Accessing values

print("Name:", student['name'])

print("Age:", student['age'])

print("Grade:", student['grade'])

Tkinter :Calculator in Python


Tkinter :Calculator in Python

 import tkinter as tk


def button_click(number):

    current = entry.get()

    entry.delete(0, tk.END)

    entry.insert(tk.END, current + number)


def button_clear():

    entry.delete(0, tk.END)


def button_equal():

    try:

        result = eval(entry.get())

        entry.delete(0, tk.END)

        entry.insert(tk.END, str(result))

    except:

        entry.delete(0, tk.END)

        entry.insert(tk.END, "Error")


def main():

    root = tk.Tk()

    root.title("Calculator")

    

    entry = tk.Entry(root, width=20, font=("Helvetica", 12))

    entry.grid(row=0, column=0, columnspan=4)


    buttons = [

        '7', '8', '9', '/',

        '4', '5', '6', '*',

        '1', '2', '3', '-',

        '0', '.', '=', '+'

    ]

    

    row = 1

    col = 0

    for button in buttons:

        if button == '=':

            tk.Button(root, text=button, width=5, command=button_equal).grid(row=row, column=col)

        elif button == 'C':

            tk.Button(root, text=button, width=5, command=button_clear).grid(row=row, column=col)

        else:

            tk.Button(root, text=button, width=5, command=lambda b=button: button_click(b)).grid(row=row, column=col)

        col += 1

        if col > 3:

            col = 0

            row += 1


    root.mainloop()


if __name__ == "__main__":

    main()


Tkinter Library Functions in Python

 Tkinter Library Functions in Python

Function/ClassDescriptionExample Usage
Tk()Creates a main windowroot = tkinter.Tk()
Label(master, options)Creates a text labellabel = tkinter.Label(root, text="Hello")
Button(master, options)Creates a buttonbutton = tkinter.Button(root, text="Click")
Entry(master, options)Creates a single-line text entryentry = tkinter.Entry(root)
Text(master, options)Creates a multi-line text areatext = tkinter.Text(root)
Frame(master, options)Creates a container for other widgetsframe = tkinter.Frame(root)
Canvas(master, options)Creates a drawing areacanvas = tkinter.Canvas(root)
Menu(master, options)Creates a menumenu = tkinter.Menu(root)
Scrollbar(master, options)Creates a scrollbarscrollbar = tkinter.Scrollbar(root)
Listbox(master, options)Creates a listbox widgetlistbox = tkinter.Listbox(root)
Checkbutton(master, options)Creates a checkbuttoncheckbutton = tkinter.Checkbutton(root, text="Check")
Radiobutton(master, options)Creates a radiobuttonradiobutton = tkinter.Radiobutton(root, text="Radio")
Scale(master, options)Creates a scale (slider) widgetscale = tkinter.Scale(root)
Spinbox(master, options)Creates a spinbox widgetspinbox = tkinter.Spinbox(root)
Toplevel(master, options)Creates a new top-level windowtop = tkinter.Toplevel(root)
PanedWindow(master, options)Creates a container for panespaned_window = tkinter.PanedWindow(root)
PhotoImage(file='path')Creates an image from a fileimage = tkinter.PhotoImage(file='image.png')
pack(options)Packs a widget into its parentbutton.pack()
grid(options)Places a widget in a gridlabel.grid(row=0, column=0)
place(options)Places a widget at specific coordinatesentry.place(x=10, y=10)
bind(event, function)Binds an event to a functionbutton.bind('<Button-1>', callback)
mainloop()Enters the Tk event looproot.mainloop()

These are just a few examples of functions and classes available in the tkinter library for building graphical user interfaces (GUIs) in Python. You can explore more options and details in the Python documentation or by using the help(tkinter) function in the Python interpreter.

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...