Solved

[python - GUI] how do I associate the checkbuttons and radio buttons to the database?

Posted on 2009-04-01
2
517 Views
Last Modified: 2012-05-06
I made a database file and I made several entries into it. Now I wanna use GUI to enter data in the database (by associating the GUI file with the database file).

In the GUI file, I have some entries like "Name", "student number" etc. and some checkbutons, I want to write a code so that if I click the submit button, the entries I made (also the checkbuttons) get stored in the database,

I want to add another button "clear", which should clear the content of the data entry fields in the GUI window, (also unselects the checkbuttons I might have)
here is my code for the GUI file: 
 
from Tkinter import *
 
def run_analyse(db):
    window = Tk()
    window.title('Database')
    
    frame = Frame(window)
    frame.pack()
    
    q1_prompt = Label(frame, text="Name:")
    q1_prompt.grid(row=0, column=0)
    q1_data = Entry(frame)
    q1_data.grid(row=0, column=1)
     
    q2_prompt = Label(frame, text="Student number:")
    q2_prompt.grid(row=2, column=0)
    q2_data = Entry(frame)
    q2_data.grid(row=2, column=1)
    
    q3_prompt = Label(frame, text="Field of study:")
    q3_prompt.grid(row=4, column=0)
    q3_data = Entry(frame)
    q3_data.grid(row=4, column=1)   
    
    q3_prompt2 = Label(frame, text="Course load")
    q3_prompt2.grid(row=5, column=0)
    
    #first check button
    x1=StringVar()
    one_course = Checkbutton(frame, text="1", variable = x1, onvalue ="1", offvalue ="")
    one_course.grid(row=6, column=1)
    
    x2=StringVar()
    two_course = Checkbutton(frame, text="2", variable = x2, onvalue ="2", offvalue ="")
    two_course.grid(row=7, column=1)
    
    x3=StringVar()
    three_course = Checkbutton(frame, text="3", variable = x3, onvalue ="3", offvalue ="")
    three_course.grid(row=8, column=1)
    
    
    x4=StringVar()
    four_course = Checkbutton(frame, text="4", variable = x4, onvalue ="4", offvalue ="")
    four_course.grid(row=9, column=1)
    
    x5=StringVar()
    five_course = Checkbutton(frame, text="5", variable = x5, onvalue ="5", offvalue ="")
    five_course.grid(row=10, column=1)
    
    window.mainloop()
    
if __name__=="__main__":
    run_analyse('testing.txt')

Open in new window

0
Comment
Question by:Student_101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24045206
You did not explain the layout of your database.txt file, the below example assumes it is comma separated. I put all the data in a global variable named "data". The name of the database is also in a global variable "db", so that it is accessible from the do_create() function.  If you define a class for your gui this can be done without using global variables.
from Tkinter import *
 
data = {}
db = 'testing.txt'
 
def run_analyse():
    global data
    window = Tk()
    window.title('Database')
    
    frame = Frame(window)
    frame.pack()
    
    q1_prompt = Label(frame, text="Name:")
    q1_prompt.grid(row=0, column=0)
    data['name'] = Entry(frame)
    data['name'].grid(row=0, column=1)
     
    q2_prompt = Label(frame, text="Student number:")
    q2_prompt.grid(row=2, column=0)
    data['nbr'] = Entry(frame)
    data['nbr'].grid(row=2, column=1)
    
    q3_prompt = Label(frame, text="Field of study:")
    q3_prompt.grid(row=4, column=0)
    data['field'] = Entry(frame)
    data['field'].grid(row=4, column=1)   
    
    q3_prompt2 = Label(frame, text="Course load")
    q3_prompt2.grid(row=5, column=0)
    
    #first check button
    data['x1']=StringVar()
    one_course = Checkbutton(frame, text="1", variable = data['x1'], onvalue ="1", offvalue ="")
    one_course.grid(row=6, column=1)
    
    data['x2']=StringVar()
    two_course = Checkbutton(frame, text="2", variable = data['x2'], onvalue ="2", offvalue ="")
    two_course.grid(row=7, column=1)
    
    data['x3']=StringVar()
    three_course = Checkbutton(frame, text="3", variable = data['x3'], onvalue ="3", offvalue ="")
    three_course.grid(row=8, column=1)
    
    
    data['x4']=StringVar()
    four_course = Checkbutton(frame, text="4", variable = data['x4'], onvalue ="4", offvalue ="")
    four_course.grid(row=9, column=1)
    
    data['x5']=StringVar()
    five_course = Checkbutton(frame, text="5", variable = data['x5'], onvalue ="5", offvalue ="")
    five_course.grid(row=10, column=1)
    
    submitbtn = Button(frame, text="Create", command=do_create)
    submitbtn.grid(row=11, column=0)
    
    clearbtn = Button(frame, text="Clear", command=do_clear)
    clearbtn.grid(row=11, column=1)
    
    window.mainloop()
 
def do_clear():
    global data
    data['name'].delete(0,END)
    data['nbr'].delete(0,END)
    data['field'].delete(0,END)
    data['x1'].set('')
    data['x2'].set('')
    data['x3'].set('')
    data['x4'].set('')
    data['x5'].set('')
 
def do_create():
    global db,data
    print 'Writing to file',db,'...'
    f=open(db,'a')
    f.write(data['name'].get()+','+
            data['nbr'].get()+','+
            data['field'].get()+','+
            data['x1'].get()+','+
            data['x2'].get()+','+
            data['x3'].get()+','+
            data['x4'].get()+','+
            data['x5'].get()+'\n')
    f.close()
    print 'Record was written!'
  
if __name__=="__main__":
    run_analyse()

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python frameworks - maybe Django. 4 116
Merge XML files using a unique key? 8 141
how to check case insensitive substring 5 76
78 files, need to delete row 2 in every file 3 30
Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question