More Samples
geometry manager.py
from tkinter import * window = Tk() #check web for more info on grid or pack manager #such as: #https://www.python-course.eu/tkinter_layout_management.php #http://effbot.org/tkinterbook/grid.htm #Warning: Never mix grid and pack in the same master window! #this app does use both in two separate frames to demo both - #don't actually do this! tkinter will crash if both conflict. #choose either pack or grid and stick to it! frame1=Frame(window) #for packing.. frame1.pack(side=TOP,fill=X) frame2=Frame(window) #for grid.. frame2.pack(side=TOP, fill=X) a = Label(frame1, text="A", bg="salmon", relief=SUNKEN) b = Label(frame1, text="B", bg="chartreuse") c = Label(frame1, text="C", bg="purple", relief=RIDGE) x = Button(frame2, text="X") y = Button(frame2, text="Y") z = Button(frame2, text="Z") a.pack(fill=X,padx=10) b.pack(fill=X,padx=10) c.pack(fill=X,padx=10) #sticky=N+S+E+W to stretch to fill grid space: x.grid(row=0,column=0, columnspan=2, sticky=N+S+E+W) y.grid(row=1,column=0) z.grid(row=1,column=1) window.mainloop()
notepad.py
from tkinter import *
from tkinter import filedialog
from tkinter import scrolledtext
window = Tk()
btnLoad = Button(text="Load a file")
btnSave = Button(text="Save to temp.txt")
txtWindow = scrolledtext.ScrolledText(wrap = WORD)
def load(event):
filename = filedialog.askopenfilename()
try:
if(filename):
txtWindow.delete(1.0, END)
for line in open(filename):
txtWindow.insert(INSERT,line)
except:
pass
def save(event):
file = open('temp.txt', 'w')
file.write(txtWindow.get(1.0, END))
file.close()
btnLoad.bind("<ButtonPress>",load)
btnSave.bind("<ButtonPress>",save)
txtWindow.pack()
btnLoad.pack()
btnSave.pack()
window.mainloop()
trader game.py
import random
from tkinter import *
#global variables:
tradeprice = 0
stocks = 0
money = 10
hours = 12
quantity = 0
#tkinter controls:
window = Tk()
lblHoursRemaining = Label(anchor=W, borderwidth=2, relief="solid", bg="green")
lblStocks = Label(anchor=W, borderwidth=2, relief="solid")
lblMoney = Label(anchor=W, borderwidth=2, relief="solid", fg="red", bg="black")
lblTradePrice = Label(anchor=W, borderwidth=2, relief="solid", bg="yellow")
lblFeedback = Label(anchor=W, borderwidth=2, relief="sunken")
btnBuy = Button(text="Buy")
btnSell = Button(text="Sell")
btnWait = Button(text="Wait 1 hr")
#need to define this command first:
def updateQuantity(qty):
global quantity
quantity = int(qty)
#can assign the command updateQuantity to the Scale now:
sclQuantity = Scale(from_=1, to=10, orient=HORIZONTAL, command=updateQuantity)
#functions:
def statusUpdate():
global money, stocks, hours, tradeprice
lblMoney.configure(text = str(money))
lblStocks.configure(text = str(stocks))
lblHoursRemaining.configure(text = str(hours))
lblTradePrice.configure(text = str(tradeprice))
if stocks > 10:
sclQuantity.configure(to = stocks)
if money % tradeprice > 10:
sclQuantity.configure(to = money % tradeprice)
def newTradePrice():
global tradeprice
tradeprice = random.randint(1,20)
def buyStocks(event):
global stocks, tradeprice, money, quantity
cost = quantity * tradeprice
if (cost <= money):
money = money - cost
stocks = stocks + quantity
outputText = "Bought " + str(quantity) + " stocks, which cost $" + str(cost)
lblFeedback.configure(text = outputText)
else:
lblFeedback.configure(text = "Insufficient funds.")
statusUpdate()
def sellStocks(event):
global stocks, tradeprice, money, quantity
if (quantity <= stocks):
profit = quantity * tradeprice
stocks = stocks - quantity
money = money + profit
outputText = "Sold " + str(quantity) + " stocks, and made $" + str(profit)
lblFeedback.configure(text = outputText)
else:
lblFeedback.configure(text = "You don't own that many stocks.")
statusUpdate()
def waitHour(event):
global hours
if hours == 1 or hours < 1: #waiting an hour with an hour left is game over:
hours = 0
statusUpdate()
lblFeedback.configure(text = "Game over.")
else:
hours -= 1
lblFeedback.configure(text = "An hour has passed.")
newTradePrice()
statusUpdate()
#bindings and pack:
btnBuy.bind("<ButtonPress>",buyStocks)
btnSell.bind("<ButtonPress>",sellStocks)
btnWait.bind("<ButtonPress>",waitHour)
Label(text="Hours Remaining:", anchor=E).grid(row=0, column=0, sticky=N+S+E+W)
lblHoursRemaining.grid(row=0, column=1, sticky=N+S+E+W, columnspan=2)
Label(text="Stocks Owned:", anchor=E).grid(row=1, column=0, sticky=N+S+E+W)
lblStocks.grid(row=1, column=1, sticky=N+S+E+W, columnspan=2)
Label(text="Money: $", anchor=E).grid(row=2, column=0, sticky=N+S+E+W)
lblMoney.grid(row=2, column=1, sticky=N+S+E+W, columnspan=2)
Label(text="Stock trading at: $", anchor=E).grid(row=3, column=0, sticky=N+S+E+W)
lblTradePrice.grid(row=3, column=1, sticky=N+S+E+W, columnspan=2)
Label(text="Quantity slider:").grid(row=4, column=0, columnspan=3, sticky=N+S+E+W)
sclQuantity.grid(row=5, column=0, columnspan=3, sticky=N+S+E+W)
btnBuy.grid(row=6, column=0, columnspan=3, sticky=N+S+E+W)
btnSell.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W)
btnWait.grid(row=8, column=0, columnspan=3, sticky=N+S+E+W)
lblFeedback.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W)
#initialise:
newTradePrice()
statusUpdate()
window.mainloop()