Hi Guys,
My final project has come and it is to make a GUI for a program that we previously wrote. So I have to use tkinter as my package for python to get this written. Now I can't find anything on tkinter and python 3.2 or any GUI "designers" for 3.2. I've seen a lot of python 2 code but none for 3. This is a really basic program and only needs 2 text input fields and a button. Would anyone help/point me in the right direction on how to start with tkinter and gui's in python 3? I will attach a code below for anyone to get the idea of the program. Thanks!

LOWER_A_ASCII = ord("a")
LOWER_Z_ASCII = ord("z")
NUM_LETTERS = 26

def getKey():
    valid = False
    while (not valid):
        try:
            key = int(input("Enter the key value (1-25): "))
            if (not 1 <= key <= 25):
                raise ValueError
            valid = True
        except ValueError:
            print("Key must be an integer between 1 and 25!")
            print("Please try again.")
    return key

def getText():
    valid = False
    while (not valid):
        text = input("Enter the text: ")
        valid = text.isalpha() and text.islower()
        if (not valid):
            print("Text must only include alphabetic lower-case letters!")
            print("Please try again.")
    return text

def encryptChar(ch, key):
    encCode = ord(ch) + key
    if (encCode > LOWER_Z_ASCII):
        encCode = encCode - NUM_LETTERS
    encChar = chr(encCode)
    return encChar

def encryptText(text, key):
    encText = ""
    for ch in text:
        encChar = encryptChar(ch, key)
        encText = encText + encChar
    return encText

def main():
    key = getKey()
    plainText = getText()
    encryptedText = encryptText(plainText, key)
    print("Plain text: %s" %plainText)
    print("Encryted text: %s" %encryptedText)

main()

Your code is not robbust, what if user inputted letter like 'lO'. For Gui, I think you only need to do initialization and replace the get_ functions with binding to input events. As for Python3 vs Pyhon2, I thin 0ython3 implementation much better as it uses new style classes. The visible use in most cases is same, and supperficial differnces should be easily corrected with running the 2to3 scriplt.

Yeah, I sort of figured it wasn't, but this is just a really simple project. There doesn't seem to be a lot of material on Tkinter that is useful... Is there some IDE/GUI environment I can use to build this or is it all just hardcoded?

You read the vegaseat's sticky thread on GUI programming at begining of the forum and many tkinter code snippets in code snippet section? (sorry my mobile phone typing mistakes, big fingers :( )

The console environment is in many ways simpler than GUI programming. To illustrate that I will show you two simple programs that do the same chores.

First the console program ...

# simple console data IO for Python3

# data in ...
text = input("Enter your name: ")

# process data string and show
s = "--> " + text
print(s)

Now the corresponding Tkinter GUI program ...

# simple Tkinter GUI data IO for Python3

import tkinter as tk

def show_data(event):
    # process entry data string and show in label
    s = "--> " + entry.get()
    label_output.configure(text=s)

# set up the root window
root = tk.Tk()

#set up the needed widgets
label_prompt = tk.Label(root, text="Enter your name:")
label_output = tk.Label(root)
entry = tk.Entry(root)

# position the widgets using grid()
label_prompt.grid(row=0, column=0)
entry.grid(row=1, column=0)
label_output.grid(row=2, column=0)

# data in ...
# give entry widget focus (cursor)
entry.focus()
# use function show_data after Return key has been pressed
entry.bind('<Return>', show_data)

# start the event loop
root.mainloop()

GUI programming has a steeper learning curve, but the graphical environment is much more pleasing to the user.

Thanks for the replies. I've decided to scrap that first project and use my very first project instead for the gui. Following some examples on the stick, I was able to sort of slap something together. Now my only problem is that I think I need to use a drop down box instead of "entry" style boxes. Is that possible with tk?

Here is my code:

import tkinter as tk
from tkinter import ttk, font 
def calculate():

    year = int(yearEn.get())
    month = int(monthEn.get())
    day = int(dayEn.get())
    hour = int(hourEn.get())
    minute = int(minuteEn.get())
    second = int(secondEn.get())
    
    totalSec=(((month*2592000)+(day*86400)+(hour*3600)+(minute*60)+(second))+1000000000)

    yearCon=(totalSec/31104000)
    yearMod=(totalSec%31104000)
    yearTot=(yearCon+year) #Special year addition.                                                    

    monthCon=(yearMod/2592000)
    monthMod=(yearMod%2592000)

    dayCon=(monthMod/86400)
    dayMod=(monthMod%86400)

    hourCon=(dayMod/3600)
    hourMod=(dayMod%3600)

    minuteCon=(hourMod/60)
    secondsCon=(hourMod%60)
    outputLb.config(text="You will be a billion seconds old: " + "%4d" % yearTot + "/" + "%2d" % monthCon + "/" + "%\
2d" % dayCon + " " + "%2d" % hourCon + ":" + "%2d" % minuteCon + ":" + "%2d" % secondsCon)

def setfocus2(event):
    monthEn.focus_set()
def setfocus3(event):
    dayEn.focus_set()
def setfocus4(event):
    hourEn.focus_set()
def setfocus5(event):
    minuteEn.focus_set()
def setfocus6(event):
    secondEn.focus_set()

root = tk.Tk()
root.title("Billion Seconds Old!")
 
yearLb = tk.Label(root, text=' Enter birth year:')
yearEn = tk.Entry(root, bg='yellow')
monthLb = tk.Label(root, text=' Enter birth month:')
monthEn = tk.Entry(root, bg='yellow')
dayLb = tk.Label(root, text=' Enter birth day:')
dayEn = tk.Entry(root, bg='yellow')
hourLb = tk.Label(root, text=' Enter birth hour:')
hourEn = tk.Entry(root, bg='yellow')
minuteLb = tk.Label(root, text=' Enter birth minute:')
minuteEn = tk.Entry(root, bg='yellow')
secondLb = tk.Label(root, text=' Enter birth second:')
secondEn = tk.Entry(root, bg='yellow')
convert = tk.Button(root, text=' Calculate ', command=calculate)
outputLb = tk.Label(root, text='')
secoutLb = tk.Label(root, text='')
introLb = tk.Label(root, text='Find out when you are one billion seconds old.',fg='blue')


introLb.grid(row=0, column=0, columnspan=3, pady=15)
yearLb.grid(row=1, column=0)
yearEn.grid(row=1, column=1)
monthLb.grid(row=2, column=0)
monthEn.grid(row=2, column=1)
dayLb.grid(row=3, column=0)
dayEn.grid(row=3, column=1)
hourLb.grid(row=4, column=0)
hourEn.grid(row=4, column=1)
minuteLb.grid(row=5, column=0)
minuteEn.grid(row=5, column=1)
secondLb.grid(row=6, column=0)
secondEn.grid(row=6, column=1)
convert.grid(row=7, column=0, pady=5)
outputLb.grid(row=8, column=0, columnspan=3)
secoutLb.grid(row=7, column=1)
 

yearEn.focus_set()
monthEn.focus_set()
dayEn.focus_set()
hourEn.focus_set()
minuteEn.focus_set()
secondEn.focus_set()

yearEn.bind("<Return>", func=setfocus2)
monthEn.bind("<Return>", func=setfocus3)
dayEn.bind("<Return>", func=setfocus4)
hourEn.bind("<Return>", func=setfocus5)
minuteEn.bind("<Return>", func=setfocus6)


root.mainloop()

ttk.Combobox looks nicer that tkinter.Optionmenu:

import ttk
import Tkinter as tkinter

root = tkinter.Tk()

style = ttk.Style()
style.theme_settings("default", {
   "TCombobox": {
       "configure": {"padding": 5},
       "map": {
           "background": [("active", "green2"),
                          ("!disabled", "green4")],
           "fieldbackground": [("!disabled", "green3")],
           "foreground": [("focus", "OliveDrab1"),
                          ("!disabled", "OliveDrab2")]
       }
   }
})

combo = ttk.Combobox(values=['apple', 'banana', 'orange']).pack()

root.mainloop()
This article has been dead for over six months. Start a new discussion instead.