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'])
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'])
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
| Function/Class | Description | Example Usage |
|---|---|---|
Tk() | Creates a main window | root = tkinter.Tk() |
Label(master, options) | Creates a text label | label = tkinter.Label(root, text="Hello") |
Button(master, options) | Creates a button | button = tkinter.Button(root, text="Click") |
Entry(master, options) | Creates a single-line text entry | entry = tkinter.Entry(root) |
Text(master, options) | Creates a multi-line text area | text = tkinter.Text(root) |
Frame(master, options) | Creates a container for other widgets | frame = tkinter.Frame(root) |
Canvas(master, options) | Creates a drawing area | canvas = tkinter.Canvas(root) |
Menu(master, options) | Creates a menu | menu = tkinter.Menu(root) |
Scrollbar(master, options) | Creates a scrollbar | scrollbar = tkinter.Scrollbar(root) |
Listbox(master, options) | Creates a listbox widget | listbox = tkinter.Listbox(root) |
Checkbutton(master, options) | Creates a checkbutton | checkbutton = tkinter.Checkbutton(root, text="Check") |
Radiobutton(master, options) | Creates a radiobutton | radiobutton = tkinter.Radiobutton(root, text="Radio") |
Scale(master, options) | Creates a scale (slider) widget | scale = tkinter.Scale(root) |
Spinbox(master, options) | Creates a spinbox widget | spinbox = tkinter.Spinbox(root) |
Toplevel(master, options) | Creates a new top-level window | top = tkinter.Toplevel(root) |
PanedWindow(master, options) | Creates a container for panes | paned_window = tkinter.PanedWindow(root) |
PhotoImage(file='path') | Creates an image from a file | image = tkinter.PhotoImage(file='image.png') |
pack(options) | Packs a widget into its parent | button.pack() |
grid(options) | Places a widget in a grid | label.grid(row=0, column=0) |
place(options) | Places a widget at specific coordinates | entry.place(x=10, y=10) |
bind(event, function) | Binds an event to a function | button.bind('<Button-1>', callback) |
mainloop() | Enters the Tk event loop | root.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.
import tkinter as tk def display_text(): text = entry.get() label.config(text="You entered: " + text) root = tk.Tk() roo...