Reputation: 71
I have used Tkinter to create radiobuttons through which I would like to increment by one in the database after the submit button is clicked whenever a radiobutton is checked.
Most important i would like to connect to MySQL database but I don't know what to add in my script.
from Tkinter import *
root= Tk()
frm=Frame(root,relief=RAISED,borderwidth=5,bg="green" )
Label(frm,text= "PLEASE SELECT CANDIDATE\n OF YOUR CHOICE\n\n\n\n",bg="green").pack()
var = IntVar()
for text, value in [('YOWERI KAGUTA MUSEVENI', 1), ('KIIZA BESIGYE', 2), ('AMAAMA JOHN MBABAZI ', 3),
('KARUNGI SHARON', 4), ('BYAMUKAMA OSCAR', 5),
('MATILDA MOREEN', 6), ('DUNCANS', 7)]:
Radiobutton(frm, text=text, value=value, variable=var,padx=18,bg="green"
).pack(anchor=E, fill=X, padx=18 )
var.set(0)
frm.pack(pady=10)
btn=Button(frm,text='submit',fg="black",bg="yellow")
btn.pack(anchor=E)
root.title("movie chooser")
root.mainloop()
Upvotes: 3
Views: 21242
Reputation: 22714
I am going to guide you step by step to resolve your problem. I assume you have already installed MySQL server on Ubuntu.
Whether you are using Tkinter or some other GUI package, the method to implement your goal remains the same.
First, you will need to to install a Python database interface that will allow you to communicate with MySQL server using Python. Here is the exhaustive list of Python MySQL databse interfaces.
Which one is better to install? I prefer to talk only about 2 of them that I used myself: MySQLdb and MySQL connector.
MySQLdb is rather a C library which makes it faster than MySQL connector which is a pure Python library. For portability it is better to choose the later one. For speed you need to choose the first one. Note that Django uses MySQLdb. That is also my favorite database interface I am going to use in what follows. You can find a good comparison between these 2 interfaces here.
The common way to install MySQLdb is to use pip as described here. But for Ubuntu, by my personal experience, I prefer to install it this way:
sudo apt-get install build-essential python-dev libmysqlclient-dev
sudo apt-get install python-mysqldb
.Now you should be able to import it:
>>> import MySQLdb
First, we'll ask the user to type his credentials to MySQL server:
If the credentials are wrong, the window still prompts itself there unless the user exists the application.
If the credentials are the right one, the user may then save/add his favorite starts to the database through a new window similar to the one you designed:
1. Database design:
I choose the simplest one, just to satisfy this immediate need:
mysql> CREATE DATABASE begueradj;
Now it is time to create the table in which we save your favorite stars. It will contains their names starname
and an auto incrementing primary key id
:
mysql> USE begueradj;
mysql> CREATE TABLE mystars(id INT(2) NOT NULL AUTO_INCREMENT,
starname VARCHAR(40) NOT NULL,
PRIMARY KEY(id)
);
2. First interface implementation:
The interface represented by the first screenshot is created by initialize_user_interface()
function:
def initialize_user_interface(self):
"""Draw a user interface allowing the user to type
MySQL server credentials
"""
self.parent.title("DB operations")
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="lavender")
self.label_user=Tkinter.Label(self.parent,text="DB User: ",anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8 bold")
self.label_password=Tkinter.Label(self.parent,text="DB Password:", anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8 bold")
self.label_user.grid(row=0,column=0,sticky=Tkinter.E+Tkinter.W)
self.label_password.grid(row=1,column=0, sticky=Tkinter.E+Tkinter.W)
self.dbuser=Tkinter.Entry(self.parent)
self.dbpassword=Tkinter.Entry(self.parent,show="*")
self.dbuser.grid(row=0,column=1,sticky=Tkinter.E+Tkinter.W)
self.dbpassword.grid(row=1,column=1,sticky=Tkinter.E+Tkinter.W)
self.connectb=Tkinter.Button(self.parent,text="Log in",font="Helvetica 10 bold",command=self.dbconnexion)
self.cancelb=Tkinter.Button(self.parent,text="Cancel",command=self.parent.quit,font="Helvetica 10 bold")
self.connectb.grid(row=2,column=1,sticky=Tkinter.W)
self.cancelb.grid(row=2,column=2)
Mainly, I bind the function dbconnexion()
to the login button self.connectb
:
def dbconnexion(self):
""" Pop up a new window if the credentials are the right ones
"""
if self.dbuser.get()=="beueradj" and self.dbpassword.get()=="begueradj":
# Pop up the new interface if credentials are OK
self.item_insertion_window()
else:
# Loop over the login interface if not
self.initialize_user_interface()
3. Second interface implementation:
If the credentials are right, then display insertion window to add the favorite stars to the database. To pop up the new window you need to use Tkinter.Toplevel()
method.
This is done by the function item_insertion_window()
:
def item_insertion_window(self):
""" Display the stars to add to the database
Group the stars using radio buttons
"""
self.new_window=Tkinter.Toplevel(self)
self.new_window.wm_title("Add my favorite stars")
self.new_window.grid_rowconfigure(0, weight=1)
self.new_window.grid_columnconfigure(0, weight=1)
self.exitb=Tkinter.Button(self.new_window,text="Exit",command=self.new_window.quit)
self.submitb=Tkinter.Button(self.new_window,text="Submit",command=self.increment_db)
self.exitb.grid(row=8,column=1)
self.submitb.grid(row=8,column=0,sticky=Tkinter.W)
self.v=IntVar()
self.tvstars=[('YOWERI KAGUTA MUSEVENI', 1), ('KIIZA BESIGYE', 2),
('AMAAMA JOHN MBABAZI ', 3), ('KARUNGI SHARON', 4),
('BYAMUKAMA OSCAR', 5), ('MATILDA MOREEN', 6),
('DUNCANS', 7)]
self.i=0
for self.txt, star in self.tvstars:
self.i=self.i+1
self.rb=Tkinter.Radiobutton(self.new_window,text=self.txt,variable=self.v,value=star)
self.rb.grid(row=self.i,column=0,sticky=Tkinter.W)
4. How to get the Tkinter radiobutton text?
In the official documentation, there is only a way to retrieve the value of the radio button which is picked, but no way is mentioned to get the text (names of the stars) which actually interests us. I did not find any post on StackOverflow dealing with this subject either. My hack is to code a dictionary to map the radio button values to the corresponding star's name using which_artist()
function:
def which_artist(self,radiob):
"""Return star's name
"""
self.artists = {
1:"YOWERI KAGUTA MUSEVENI",
2:"KIIZA BESIGYE",
3:"AMAAMA JOHN MBABAZI",
4:"KARUNGI SHARON",
5:"BYAMUKAMA OSCAR",
6:"MATILDA MOREEN",
7:"DUNCANS",
}
return self.artists.get(radiob,"Unknown")
This function will be useful in the following step.
5. Adding your favorite star to the database:
First, you may save your MySQL server parameters in a configuration file config.py
, but as our project is small and apparently you are not going to extend it any further in the futur, let's save these credentials within a function itself:
self.config = {
'user': 'begueradj',
'passwd': 'begueradj',
'host': '127.0.0.1',
'db': 'begueradj',
}
Here is the function that implements the insertion:
def increment_db(self):
""" Insert the selected favorite star into the database.
"""
self.chosenartist=self.which_artist(self.v.get())
print self.chosenartist
self.config = {
'user': 'begueradj',
'passwd': 'bregredj',
'host': '127.0.0.1',
'db': 'begueradj',
}
try:
self.connecttodb=MySQLdb.connect(**self.config)
except MySQLdb.Error:
print"Connexion error"
self.cursor=self.connecttodb.cursor()
self.cursor.execute("""INSERT INTO testtable(starname) VALUES(%s)""",self.chosenartist)
self.connecttodb.commit()
self.connecttodb.close()
Of course, you will need to change the configuration dictionary self.config
according to your own settings.
Also, it is more suitable to bind self.connecttodb.close()
to the exit button self.exitb
instead.
6. The application:
Here is the full program:
'''
Created on Feb 29, 2016
@author: begueradj
'''
import Tkinter
import MySQLdb
from Tkinter import IntVar
class Begueradj(Tkinter.Frame):
'''
classdocs
'''
def __init__(self, parent):
'''
Constructor
'''
Tkinter.Frame.__init__(self, parent)
self.parent=parent
self.initialize_user_interface()
def initialize_user_interface(self):
"""Draw a user interface allowing the user to type
MySQL server credentials
"""
self.parent.title("DB operations")
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="lavender")
self.label_user=Tkinter.Label(self.parent,text="DB User: ",anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8 bold")
self.label_password=Tkinter.Label(self.parent,text="DB Password:", anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8 bold")
self.label_user.grid(row=0,column=0,sticky=Tkinter.E+Tkinter.W)
self.label_password.grid(row=1,column=0, sticky=Tkinter.E+Tkinter.W)
self.dbuser=Tkinter.Entry(self.parent)
self.dbpassword=Tkinter.Entry(self.parent,show="*")
self.dbuser.grid(row=0,column=1,sticky=Tkinter.E+Tkinter.W)
self.dbpassword.grid(row=1,column=1,sticky=Tkinter.E+Tkinter.W)
self.connectb=Tkinter.Button(self.parent,text="Log in",font="Helvetica 10 bold",command=self.dbconnexion)
self.cancelb=Tkinter.Button(self.parent,text="Cancel",command=self.parent.quit,font="Helvetica 10 bold")
self.connectb.grid(row=2,column=1,sticky=Tkinter.W)
self.cancelb.grid(row=2,column=2)
def item_insertion_window(self):
self.new_window=Tkinter.Toplevel(self)
self.new_window.wm_title("Add my favorite stars")
self.new_window.grid_rowconfigure(0, weight=1)
self.new_window.grid_columnconfigure(0, weight=1)
self.exitb=Tkinter.Button(self.new_window,text="Exit",command=self.new_window.quit)
self.submitb=Tkinter.Button(self.new_window,text="Submit",command=self.increment_db)
self.exitb.grid(row=8,column=1)
self.submitb.grid(row=8,column=0,sticky=Tkinter.W)
self.v=IntVar()
self.tvstars=[('YOWERI KAGUTA MUSEVENI', 1), ('KIIZA BESIGYE', 2),
('AMAAMA JOHN MBABAZI ', 3), ('KARUNGI SHARON', 4),
('BYAMUKAMA OSCAR', 5), ('MATILDA MOREEN', 6),
('DUNCANS', 7)]
self.i=0
for self.txt, star in self.tvstars:
self.i=self.i+1
self.rb=Tkinter.Radiobutton(self.new_window,text=self.txt,variable=self.v,value=star)
self.rb.grid(row=self.i,column=0,sticky=Tkinter.W)
def which_artist(self,radiob):
self.artists = {
1:"YOWERI KAGUTA MUSEVENI",
2:"KIIZA BESIGYE",
3:"AMAAMA JOHN MBABAZI",
4:"KARUNGI SHARON",
5:"BYAMUKAMA OSCAR",
6:"MATILDA MOREEN",
7:"DUNCANS",
}
return self.artists.get(radiob,"Unknown")
def increment_db(self):
#print self.v.get()
self.chosenartist=self.which_artist(self.v.get())
print self.chosenartist
self.config = {
'user': 'begueradj',
'passwd': 'begueradj',
'host': '127.0.0.1',
'db': 'begueradj',
}
try:
self.connecttodb=MySQLdb.connect(**self.config)
except MySQLdb.Error:
print"Connexion error"
self.cursor=self.connecttodb.cursor()
self.cursor.execute("""INSERT INTO mystars(starname) VALUES(%s)""",self.chosenartist)
self.connecttodb.commit()
self.connecttodb.close()
def dbconnexion(self):
if self.dbuser.get()=="begueradj" and self.dbpassword.get()=="begueradj":
self.item_insertion_window()
else:
self.initialize_user_interface()
def main():
root=Tkinter.Tk()
d=Begueradj(root)
root.mainloop()
if __name__=="__main__":
main()
7. Demo
I inserted one star through the Tkinter interface. Let's check if that was effective:
mysql> SELECT * FROM begueradj.mystars;
+----+------------------------+
| id | starname |
+----+------------------------+
| 1 | YOWERI KAGUTA MUSEVENI |
+----+------------------------+
1 row in set (0.00 sec)
Hope this helps.
You asked me about how to update but your comments are not clear to me when it comes to understanding the logic of your update statement. So I am answering here only according to the small piece of code your comment shows.
You seem to want to check which radio button is selected through your statement: self.var.get()
and increment something by 1 (and that is why I do not understand your update statement: so whatever the radiobutton which is pressed you do the same action; which thing means the radio buttons are meaningless).
If you want to do that in the perspective of some mysterious test of your own though, you will need to use variable
option and bind a method to the command
option for each radiobutton you created.
To use the variable
option, you will need to run this first:
self.v = IntVar()
Then, for each radiobutton you set: variable=self.v
and command = self.get_radiobutton_id
The method in question must return the identifier of the selected radiobutton:
self defself.get_radiobutton_id(self):
return v.get()
Upvotes: 4