Untitled

from tkinter import *
from tkinter import ttk

def calculate():
    v1 = float(in1.get())
    v2 = float(in2.get())
    if combo1.current() == 0:
        lab1["text"] = str(v1 + v2)
    elif combo1.current() == 1:
        lab1["text"] = str(v1 - v2)
    elif combo1.current() == 2:
        lab1["text"] = str(v1 * v2)
    elif combo1.current() == 3:
        lab1["text"] = str(v1 / v2)

root = Tk()
root.title("Entry Demo")
root.geometry("500x100+200+100")
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)   
root.columnconfigure(1, weight=1)   
root.columnconfigure(2, weight=1)   
root.columnconfigure(3, weight=1)   
root.columnconfigure(4, weight=1)   
in1 = Entry(root, width=10)
in1.grid(row=0, column=0,padx=2,pady=5)

    
combo1 = ttk.Combobox(root, values=["+", "-", "X", "/"],
                       width=3)
combo1.current(0)
combo1.grid(row=0, column=1, pady=5,padx=2)

in2 = Entry(root, width=10)
in2.grid(row=0, column=2, pady=5, padx=2)

eqBtn = Button(root, text="=",
               command=calculate)
eqBtn.grid(row=0, column=3)

lab1 = Label(root, text='0')
lab1.grid(row=0,column=4,padx=2,pady=5)

root.mainloop()

#sample entry demo given by teacher
from tkinter import *

fontSize = 16

def save_text():
    outFile = open("output.txt", "w")#w means write out, r = read in
    outFile.write(text1.get("1.0", END))#1.0 is to start from where, END is to write it all, eg is u want only 10 words, then limit it (same format)
    outFile.close()#must close, cz back in my days, we avoid to constany touch the hard dri
    
    
def enlargeFont():
    global fontSize
    fontSize += 2
    text1.configure(font=("Time New Romans",fontSize, ""))

def reduceFont():
    global fontSize
    fontSize -= 2
    text1.configure(font=("Time New Romans",fontSize, ""))   
    
    
root = Tk()
root.title("Entry Demo")
root.geometry("500x600+200+100")
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
root.rowconfigure(2, weight=1)
root.rowconfigure(3, weight=1)
root.rowconfigure(4, weight=1)
root.rowconfigure(5, weight=1)
root.rowconfigure(6, weight=1)
root.columnconfigure(0, weight=1)   
root.columnconfigure(1, weight=1)   
root.columnconfigure(2, weight=1)   
root.columnconfigure(3, weight=1)   
# root.columncnfigure(4, weight=1)

btnEnlarge = Button(root, text="Enlarge",
               command=enlargeFont)
btnEnlarge.grid(row=0, column=0, sticky=NSEW)
btnReduce = Button(root, text="Reduce",
               command=reduceFont)
btnReduce.grid(row=0, column=1, sticky=NSEW)
btnSave = Button(root, text="Save",
                 command=save_text)
btnSave.grid(row=0, column=2, sticky=NSEW)
btnExit = Button(root, text="Exit",
                 bg='#aaccdd',
                 command=root.destroy)
btnExit.grid(row=0,column=3,sticky=NSEW)

text1 = Text(root, font=("Time New Romans", fontSize, ""))
text1.grid(row=1,column=0,columnspan=4,rowspan=6,sticky=NSEW)

root.mainloop()
#font enlarger/ reducer

Untitled

Untitled

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("GUI Demo")
root.geometry("600x500+200+100")

def openNewWindow():
    root.withdraw()
    newWindow = Toplevel( root )
    newWindow.title("New Window")
    newWindow.geometry("400x300")
    Label(newWindow, text="This is a new Window").pack()
    btn = Button( newWindow,
                 text="Close",
                 command = lambda:newWindowClose(newWindow))
    btn.pack(pady=5)

def newWindowClose(self):
    root.deiconify()
    self.destroy()
    
lab1 = Label( root, text="This is the main window")
lab1.pack(pady=10)
btn = Button( root,
             text="Click to open a new window",
             command = openNewWindow)
btn.pack( pady = 10)

root.mainloop()
#this program  enables user to hide closed window/ store previous window
#demo program, given by the teacher

Untitled