Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_demos.py -- Basic Level
- from Tkinter import *
- root = Tk()
- theLabel = Label(root, text="A Tkinter Window in Python")
- theLabel.pack()
- Lab2 = Label(root, text="This is Label #2")
- Lab2.pack()
- root.mainloop()
- theLabel = Label(None, text="Click Me", fg="blue")
- theLabel.pack()
- Btn2 = Button(None, text="Button #2*", fg="red")
- Btn2.pack()
- root.mainloop()
- root = Tk()
- frameA = Frame(root)
- frameA.pack()
- frameB = Frame(root)
- frameB.pack(side=BOTTOM)
- BtnA = Label(frameA, text="Button #1", fg="blue")
- BtnA.pack()
- BtnB = Label(frameA, text="Button #2", fg="red")
- BtnB.pack()
- BtnC = Label(frameB, text="Button #3", fg="purple")
- BtnC.pack()
- BtnD = Label(frameB, text="Button #4", fg="green")
- BtnD.pack()
- root.mainloop()
- root = Tk()
- frameA = Frame(root)
- frameA.pack()
- frameB = Frame(root)
- frameB.pack(side=BOTTOM)
- BtnA = Label(frameA, text="Button #1", fg="blue")
- BtnA.pack(side=LEFT)
- BtnB = Label(frameA, text="Button #2", fg="red")
- BtnB.pack(side=LEFT)
- BtnC = Label(frameB, text="Button #3", fg="purple")
- BtnC.pack(side=LEFT)
- BtnD = Label(frameB, text="Button #4", fg="green")
- BtnD.pack(side=LEFT)
- root.mainloop()
- root = Tk()
- BtnA = Label(None, text="Button #1", bg="blue")
- BtnA.pack()
- BtnB = Label(None, text="Button #2", bg="red")
- BtnB.pack(fill=X)
- BtnC = Label(None, text="Button #3", bg="purple")
- BtnC.pack(side=LEFT, fill=Y)
- BtnD = Label(None, text="Button #4", bg="green")
- BtnD.pack(expand=True, fill=BOTH)
- root.mainloop()
- root = Tk()
- LabelA = Label(None, text="Label #1", bg="blue")
- LabelB = Label(None, text="Label #2", bg="red")
- LabelC = Label(None, text="Label #3", bg="purple")
- LabelD = Label(None, text="Label #4", bg="green")
- LabelA.grid(row=0, column=1)
- LabelB.grid(row=1, column=0)
- LabelC.grid(row=1, column=3)
- LabelD.grid(row=2, column=2)
- root.mainloop()
- root = Tk()
- theLabel = Label(None, text="Enter Username: ", fg="blue")
- theLabel.grid(row=0, column=0)
- theEntry = Entry(root)
- theEntry.grid(row=0, column=1)
- root.mainloop()
- root = Tk()
- theLabel = Label(None, text="Enter Username: ", fg="blue")
- theLabel.grid(row=0, column=0)
- theEntry = Entry(root)
- theEntry.grid(row=0, column=1)
- Lab2 = Label(None, text="Password: ", fg="blue")
- Lab2.grid(row=1, column=0)
- Entry2 = Entry(root)
- Entry2.grid(row=1, column=1)
- theChkBtn = Checkbutton(root, text="Remember Password ")
- theChkBtn.grid(columnspan=2)
- root.mainloop()
- root = Tk()
- def sayHi():
- print "Hello World! \n -- It's Python\n"
- theButton = Button(None, text="Click Me", fg="blue", command=sayHi)
- theButton.pack()
- root.mainloop()
- root = Tk()
- root.geometry('400x400+1+1')
- def sayHi(*arg):
- print "Thanks, You Are Awesome!\n"
- theLabel = Label(root, text="Please Click Anywhere On The Canvas", fg="blue")
- theLabel.pack()
- root.bind('<Button-1>', sayHi)
- root.mainloop()
- root = Tk()
- root.geometry('200x200+1+1')
- def leftClk(event):
- print "Left Click"
- def rightClk(event):
- print "Right Click"
- def spacebar(event):
- print "Spacebar"
- def leftKey(event):
- print "Left Arrow"
- def rightKey(event):
- print "Right Arrow"
- theLabel = Label(root, text="*** Python Bindings ***", fg="blue")
- theLabel.pack()
- root.bind('<Button-1>', leftClk)
- root.bind('<Button-3>', rightClk)
- root.bind('<space>', spacebar)
- root.bind('<Left>', leftKey)
- root.bind('<Right>', rightKey)
- root.mainloop()
- root = Tk()
- msg=''' *** Basic Calculator (BEDMAS) *** \n\nNote:\nAlso try 7+2*10
- then (7+2)*10\nWhereas...\n1st answer will be 27\n2nd answer will be 90\n'''
- top = Label(root, text=msg, fg="blue")
- top.pack()
- def evaluate(event):
- data = e.get()
- ans.configure(text='Answer: '+str(eval(data)))
- e = Entry(root)
- e.bind('<Return>', evaluate)
- e.pack(fill=X)
- ans = Label(root, text='Press Enter To Calculate')
- ans.pack()
- root.mainloop()
- import tkMessageBox
- tkMessageBox.showinfo("This Is The Window Title", "Hello Python Message Box")
- for z in 'z'*3:
- ans = tkMessageBox.askquestion("This Will Loop 3 Times", "Are You A Person?")
- if ans == 'yes':
- reply = "Hello Human!"
- if ans == 'no':
- reply = "Hello Non-Human Entity!"
- #
- tkMessageBox.showinfo("Message Box Demo", reply)
- root = Tk()
- root.geometry('500x500+1+1')
- def mbox(reply):
- tkMessageBox.showinfo("Submenu Demo", reply)
- def A():
- mbox("Hello A!")
- def B():
- mbox("Hey B!")
- def C():
- mbox("Hi C!")
- def X():
- mbox("Greetings X!")
- def Y():
- mbox("Bye Y!")
- def Z():
- mbox("Welcome Onboard Z!")
- def about():
- mbox("This Is Python In Tkinter!")
- menubar = Menu(root)
- # create a pulldown menu, and add it to the menu bar
- submenuABC = Menu(menubar, tearoff=0)
- submenuABC.add_command(label="Option A", command=A)
- submenuABC.add_command(label="Option B", command=B)
- submenuABC.add_command(label="Option C", command=C)
- submenuABC.add_separator()
- submenuABC.add_command(label="** Exit **", command=root.destroy)
- menubar.add_cascade(label="ABC", menu=submenuABC)
- # create more pulldown menus
- submenuXYZ = Menu(menubar, tearoff=0)
- submenuXYZ.add_command(label="Option X", command=X)
- submenuXYZ.add_command(label="Option Y", command=Y)
- submenuXYZ.add_command(label="Option Z", command=Z)
- menubar.add_cascade(label="XYZ", menu=submenuXYZ)
- helpmenu = Menu(menubar, tearoff=0)
- helpmenu.add_command(label="About", command=about)
- menubar.add_cascade(label="Help", menu=helpmenu)
- # display the menu
- root.config(menu=menubar)
- root.mainloop()
- root = Tk()
- class Cv():
- outp = ''
- cv = Cv()
- calc = StringVar()
- calc.set('9+21')
- ans = Label(root, textvariable=calc)
- ans.grid(columnspan=4)
- def keypress(keyp):
- if keyp or cv.outp:
- cv.outp = cv.outp + str(keyp)
- calc.set(cv.outp)
- def equalbtn(*arg):
- cv.outp = '1.0*{}'.format(cv.outp)
- cv.outp = str(eval(cv.outp))
- calc.set(cv.outp)
- def reset(*arg):
- cv.outp = ''
- calc.set('0')
- sym = 'DIVIDE / MULTIPLY * SUBSTRACT - ADD +'.split()
- Btn = range(10)
- for z in Btn:
- Btn[z] = Button(root, text='\n {} \n'.format(z), fg="blue", width=12, command=lambda z=z:keypress(z))
- if not z: Btn[0].grid(row=4, column=1)
- else: Btn[z].grid(row=((z-1)/3)+1, column=(z-1)%3)
- #
- z=1
- while sym:
- oper,sign=sym.pop(0),sym.pop(0)
- Btn.append('')
- Btn[len(Btn)-1] = Button(root, text='\n {} \n'.format(oper), fg="red", width=12, command=lambda s=sign:keypress(s))
- Btn[len(Btn)-1].grid(row=z, column=3)
- z+=1
- equal = Button(root, text='\n = \n', width=12, command=equalbtn)
- equal.grid(row=4, column=2)
- reset = Button(root, text='\n C \n', fg="red", width=12, command=reset)
- reset.grid(row=4, column=0)
- root.mainloop()
- root = Tk()
- canvas = Canvas(root, width=600, height=600)
- canvas.pack()
- x,y = 60,100
- canvas.create_oval((x,y,x+400,y+270), fill='green')
- canvas.create_polygon(60,100, 500,200, 580,400, 20,580, 480,300, fill='gray')
- canvas.create_rectangle(20,20,250,580)
- root.mainloop()
- root = Tk()
- import random, time
- canvas = Canvas(root, width=600, height=600)
- canvas.pack()
- colors = 'red orange yellow green blue purple gray aqua lightpink'.split()
- def oRect(x,y,x2,y2,color=None):
- canvas.create_rectangle((x,y,x2,y2),fill=color)
- #
- xy = []
- for x in range(1,6):
- for y in range(1,6):
- xy += [[x*100,y*100]]
- #
- print xy
- xy2 = []
- while 1:
- canvas.delete('all')
- random.shuffle(xy) # Note: "xy = random.shuffle(xy)" will return "None"
- xy2 = xy[:]
- for color in colors:
- x,y = xy2.pop()
- x -= random.randint(0,30)
- y -= random.randint(0,30)
- x2 = x + random.randint(20,60)
- y2 = y + random.randint(20,60)
- oRect(x,y,x2,y2,color)
- canvas.update()
- t = time.time() + 0.4
- while t > time.time(): 0
- root.mainloop()
Add Comment
Please, Sign In to add comment