Sunday, February 7, 2021

Menu Widget

menu_frame2.py


#!/usr/bin/python3


from tkinter import *

#from tkinter import ttk


root = Tk()

root.title("Controls")

root.geometry("400x400+10+100")


my_menu = Menu(root)

root.config(menu=my_menu)


def our_command(my_text):

    hide_all_frames()

    my_label.config(text=my_text)

    my_label.pack()


#file_new function

def option_red():

    hide_all_frames()

    option_red_frame.pack(fill="both", expand=1)

    my_label=Label(option_red_frame, text="Option->Red")

    my_label.pack()


#edit_cut function

def edit_blue():

    hide_all_frames()

    edit_blue_frame.pack(fill="both", expand=1)

    my_label=Label(edit_blue_frame, text="Edit->Blue")

    my_label.pack()


#hide all frames function

def hide_all_frames():

    my_label.forget()


    #loop thru all chilern in frame and delete them

    for widget in edit_blue_frame.winfo_children():

        widget.destroy()

    for widget in option_red_frame.winfo_children():

        widget.destroy()


    edit_blue_frame.pack_forget()

    option_red_frame.pack_forget()


#create a new item

file_menu = Menu(my_menu)

my_menu.add_cascade(label="File", menu=file_menu)

file_menu.add_command(label="New...", command=lambda: our_command("New"))

file_menu.add_separator()

file_menu.add_command(label="Exit", command=root.quit)


#create Edit Menu item

edit_menu = Menu(my_menu)

my_menu.add_cascade(label="Edit", menu=edit_menu)

edit_menu.add_command(label="Cut", command=lambda: our_command("Cut"))

edit_menu.add_command(label="Copy", command=lambda: our_command("Copy"))

edit_menu.add_separator()

edit_menu.add_command(label="Blue", command=edit_blue)


#create Optiion Menu item

option_menu = Menu(my_menu)

my_menu.add_cascade(label="Option", menu=option_menu)

option_menu.add_command(label="Find", command=lambda: our_command("Find"))

option_menu.add_command(label="Find Next", command=lambda: our_command("Find Next"))

option_menu.add_separator()

option_menu.add_command(label="Red", command=option_red)


#create frames

edit_blue_frame = Frame(root, width=400, height=400, bg="blue")

option_red_frame = Frame(root, width=400, height=400, bg="red")


my_label=Label(root)

my_label.pack()


root.mainloop()

 

Listbox Widget

 listbox.py


#!/usr/bin/env python3


from tkinter import *


root = Tk()

root.title("ListBox")

root.geometry("400x400+10+100")


#create frame and scrollbar

my_frame = Frame(root)


my_scrollbar = Scrollbar(my_frame, orient=VERTICAL)


#Listbox!

# SINGLE, BROWSE, MULTIPLE, EXTENDED

my_listbox = Listbox(my_frame,height=5, yscrollcommand = my_scrollbar.set, selectmode=MULTIPLE)


my_scrollbar.config(command=my_listbox.yview)

my_scrollbar.pack(side=RIGHT, fill=Y)

my_frame.pack()

my_listbox.pack(pady=15)


#add item to ListBox

my_listbox.insert(END,"Item#1")

my_listbox.insert(END,"Item#2")

my_listbox.insert(0,"Item#3")


#add list of itmes

my_list = ["One", "Two", "Three","One", "Two", "Three","One", "Two", "Three"]


for item in my_list:

    my_listbox.insert(END, item)


my_listbox.insert(3,"New Item")


def delete():

    my_listbox.delete(ANCHOR)

    my_label.config(text="")


def delete_all():

    my_listbox.delete(0,END)

    my_label.config(text="")


def select():

    my_label.config(text=my_listbox.get(ANCHOR))


def select_all():

    result = ""

    for item in my_listbox.curselection():

        result = result + str(my_listbox.get(item)) + '\n'

    my_label.config(text=result)


def delete_multiple():

    for item in reversed(my_listbox.curselection()):

        my_listbox.delete(item)

        my_label.config(text="")


my_button = Button(root, text="Delete", command=delete)

#my_button.pack(pady=5)


my_button2 = Button(root, text="Select", command=select)

#my_button2.pack(pady=5)


global my_label

my_label=Label(root,text="")

my_label.pack(pady=5)


my_button3 = Button(root, text="Delete All", command=delete_all)

my_button3.pack(pady=5)


my_button4 = Button(root, text="Select All", command=select_all)

my_button4.pack(pady=5)


my_button5 = Button(root, text="Delete Multiple", command=delete_multiple)

my_button5.pack(pady=5)


root.mainloop()


Frame Widget

frame.py


from tkinter import *

#from PIL import ImageTk,Image


root = Tk()

#root.iconbitmap('@miscA38.xbm')


frame = LabelFrame(root, text='My Frame', padx=50, pady=50)

frame.pack(padx=10,pady=10)


frame2 = Frame(root, padx=50, pady=50,bg="red")

frame2.pack(padx=10,pady=10)


b1 = Button(frame, text="Hi there")

b2 = Button(frame, text="Hi there")

b1.grid(row=0,column=0)

b2.grid(row=1,column=1)


b3 = Button(frame2, text="Hi there")

b4 = Button(frame2, text="Hi there")

b3.grid(row=0,column=0)

b4.grid(row=1,column=1)


root.mainloop()


Entry Widget

 entry.py


#!/usr/bin/python3


from tkinter import *


root =Tk()


myLabel = Label(root)


e = Entry(root, width=50, bg='black', fg='white')

e.pack()

e.insert(0,"Tommy")


#button Function

def myClick():

    global myLabel

    myLabel.destroy()

    Hello = "Hello " + e.get()

    myLabel = Label(root, text=Hello)

    myLabel.pack()


#button calls Function - MyClick

myButton = Button(root, text="Type your name,", command=myClick)


#put Buttons in Window-root

myButton.pack()


root.mainloop( )


Checkbutton Widget

 check.py


#!/usr/bin/env python3


from tkinter import *


root =Tk()

root.geometry("400x400")


def show():

    my_label = Label(root, text=var.get()).pack()


var = StringVar()


c=Checkbutton(root, text="Here", variable=var, onvalue = "On", offvalue = "Off", command=show)

c.deselect()

c.pack()


my_button = Button(root, text="show", command=show).pack()


root.mainloop()


Canvas Widget

canvas.py


 #!/usr/bin/env python3


from tkinter import *


root = Tk()

root.title("Canvas")

root.geometry("500x500+10+100")

root.configure(bg='bisque')

can_height = 300

my_canvas = Canvas(root,width=300, height=can_height, bg='white')

my_canvas.pack(pady=20)


#create Rectangle

#my_rectangle.create_line(x1, y1, x2, y2, fill="color")

my_canvas.create_rectangle(10, can_height-0, 200, can_height-100, fill="pink")


#create Oval

#my_canvas.create_oval(x1, y1, x2, y2, fill="color")

my_canvas.create_oval(10, can_height-0, 200, can_height-100, fill="cyan")


#create Line

#my_canvas.create_line(x1, y1, x2, y2, fill="color")

my_canvas.create_line(10, can_height-0, 200, can_height-100, fill="red")


root.mainloop()


Button Widget

buttons.py


 #!/usr/bin/env python3


from tkinter import *


root =Tk()


#button Function

def myClick():

    myLabel = Label(root, text="Look I clicked a Button!")

    myLabel.pack()


#disable a button

myButton1 = Button(root, text="Click Me!", state=DISABLED)

#make button bigger and add color to text(fg)/button(bg)

myButton2 = Button(root, text="Click Me!", padx=50, pady=50, fg='white', bg='black')

#button calls Function - MyClick

myButton3 = Button(root, text="Click Me!", command=myClick)


#put Buttons in Window-root

myButton1.pack()

myButton2.pack()

myButton3.pack()


root.mainloop( )