Chapter 14: Graphical User Interfaces (Solutions to Even-Numbered Exercises)

Question 2

from Tkinter import *

def increase(value):
    value.set(value.get() + 1)

window = Tk()
value = IntVar()
button = Button(window, textvariable=value, command=lambda: increase(value))
button.pack()

window.mainloop()

Question 4

from Tkinter import *

def dnacount(dna, count):
    a = 0
    t = 0
    c = 0
    g = 0
    sequence = dna.get(0.0, END)
    for letter in sequence:
        if letter == "A":
            a += 1
        elif letter == "T":
            t += 1
        elif letter == "C":
            c += 1
        elif letter == "G":
            g +=1
    count.set("Num As: " + str(a) + " Num Ts:" + str(t) + " Num Cs:" + str(c) + " Num Gs:" + str(g))
    
window = Tk()
frame = Frame(window)
frame.pack()
dna = Text(frame, height=8, width=30)
dna.pack()
button = Button(frame, text="Count", command=lambda: dnacount(dna, count))
button.pack()
count = StringVar()
result = Label(frame, textvariable=count)
result.pack()

window.mainloop()