1 Replies - 501 Views - Last Post: 15 December 2015 - 12:51 PM Rate Topic: -----

#1 ktt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 15

PYTHON GUI HELP

Posted 14 December 2015 - 09:31 PM

Hey guys, I'm currently in school learning about python and one of my last assignments is to make a simple gui. I added in the radiobutton app and as added in color to the background it stopped working properly. There is 4 options of the radiobutton to click, when i run the code 3/4 of the options are already selected. How can I fix it to make it only save the option that I click?
Please download the attached file to run this code. Help will be much appreciated!

from tkinter import *

def save_data():
    fileD = open("Flightlist.txt", "a")
    fileD.write("Depot:\n")
    fileD.write("%s\n" % depot.get())
    fileD.write("Description:\n")
    fileD.write("%s\n" % description.get())
    fileD.write("Address:\n")
    fileD.write("%s\n" % address.get("1.0", END))    
    depot.set(None)
    description.delete(0, END)
    address.delete("1.0", END)

def read_fly(file):
    fly = []
    fly_f = open(file)
    for line in fly_f:
        fly.append(line.rstrip())
    return fly

app = Tk()
app.title('Cheaper Than Cheap Airlines')
Label(app, text = "Flight Ticket to :").pack()
air = StringVar()
air.set(None)
options = read_fly("Flightform.txt")
OptionMenu(app, air, *options).pack()
Label(app, text = "Name:").pack()
description = Entry(app)
description.pack()
Label(app, text = "Address:").pack()
address = Entry(app)
address.pack()

air = StringVar()

air.set(None)
w = Radiobutton(app, text="Beyond First Class ($200)", bg="Light Blue", fg="Blue")
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="First Class ($100)", bg="Light Blue", fg="Black")
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="Economy($50)", bg="Light blue", fg="white")
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="Bulk Cargo Hold(Free)", bg="Light blue", fg="red")
w.pack(fill=X,padx=10)

Label(app, text = "Description:").pack()
description = Entry(app)
description.pack()

Button(app, text = "Save", command = save_data).pack()
app.mainloop()

This post has been edited by andrewsw: 15 December 2015 - 12:20 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: PYTHON GUI HELP

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,926
  • Joined: 16-October 07

Re: PYTHON GUI HELP

Posted 15 December 2015 - 12:51 PM

Your basic problem for radio buttons is that they have to have a common variable.

Review the changes in this code:
from Tkinter import *

def save_data():
    print "save_data called"

def get_options():
    return [ "First Class", "Steerage" ] # read_fly("Flightform.txt")

app = Tk()
app.title('Cheaper Than Cheap Airlines')

Label(app, text = "Flight Ticket to :").pack()
air = StringVar()
air.set(None)
options = get_options()
OptionMenu(app, air, *options).pack()

Label(app, text = "Name:").pack()
description = Entry(app)
description.pack()

Label(app, text = "Address:").pack()
address = Entry(app)
address.pack()

# wtf, you already used this variable name
# air = StringVar()
# air.set(None)

# you need a variable to unite the radio buttons
button_val = IntVar()
button_val.set(0) # light up the first one
# a command to fire when the button has changed
def button_cmd():
    print "button_cmd", button_val.get()

w = Radiobutton(app, text="Beyond First Class ($200)", bg="Light Blue", fg="Blue", variable=button_val, value=0, command=button_cmd)
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="First Class ($100)", bg="Light Blue", fg="Black", variable=button_val, value=1, command=button_cmd)
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="Economy($50)", bg="Light blue", fg="white", variable=button_val, value=2, command=button_cmd)
w.pack(fill=X,padx=10)
w = Radiobutton(app, text="Bulk Cargo Hold(Free)", bg="Light blue", fg="red", variable=button_val, value=3, command=button_cmd)
w.pack(fill=X,padx=10)

Label(app, text = "Description:").pack()
description = Entry(app)
description.pack()

Button(app, text = "Save", command = save_data).pack()
app.mainloop()



However, your code is kind of all over the place. You need to keep track of all those values you have floating about so you can access them after you've created your form. Consider using the oop approach to control your chaos: https://docs.python....ry/tkinter.html

Hope this helps.

This post has been edited by baavgai: 15 December 2015 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1