Hi All,
In this tutorial we will update list as user enter an entry to the list. We create an entry field and a button to get details from the user and label to show the list. First we will create a user interface.
1) First step is to create a non resizable window 600X400 :-
#!/usr/bin/python
import sys
if sys.version_info < (3, 0):
# Python 2
import Tkinter as tk
from Tkinter import *
else:
# Python 3
import tkinter as tk
from Tkinter import *
root = tk.Tk()
root.title("Mac Application")
root.resizable(width=False, height=False)
root.geometry('600x400')
obj = ListExample()
tk.mainloop()
2) Now create user interface for app : We added a label , textfield and a button , to create UI we used tkinter library of python.
#!/usr/bin/python
import sys
if sys.version_info < (3, 0):
# Python 2
import Tkinter as tk
from Tkinter import *
else:
# Python 3
import tkinter as tk
from Tkinter import *
class ListExample:
def __init__(self):
parent = tk.Frame(root)
parent.grid()
self.v = StringVar()
self.v.set('')
self.e2 = tk.Entry(parent , bd = 2,textvariable=self.v)
self.e2.grid(row=0, column=0)
self.e2.pack(side="left",fill="x", expand=True)
self.btn = tk.Button(parent, text="Add Player")
self.btn.grid(row=0, column=1)
self.btn.pack(side="right",fill="x", expand=True)
parent.pack(expand=0)
self.lbl = tk.Label( root , bd=2 , text="")
self.lbl.pack()
self.lbl1 = tk.Label( root , bd=2 , text="Players :- ")
self.lbl1.pack()
self.mylist = []
self.lbl2 = tk.Label( root, bd=2 , text="")
self.lbl2.pack()
root = tk.Tk()
root.title("Mac Application")
root.resizable(width=False, height=False)
root.geometry('600x400')
obj = ListExample()
tk.mainloop()
3) Now add action to button and show the status of user action below :-
#!/usr/bin/python
import sys
if sys.version_info < (3, 0):
# Python 2
import Tkinter as tk
from Tkinter import *
else:
# Python 3
import tkinter as tk
from Tkinter import *
class ListExample:
def __init__(self):
parent = tk.Frame(root)
parent.grid()
self.v = StringVar()
self.v.set('')
self.e2 = tk.Entry(parent , bd = 2,textvariable=self.v)
self.e2.grid(row=0, column=0)
self.e2.pack(side="left",fill="x", expand=True)
self.btn = tk.Button(parent, text="Add Player",command=self.addPlayer)
self.btn.grid(row=0, column=1)
self.btn.pack(side="right",fill="x", expand=True)
parent.pack(expand=0)
self.lbl = tk.Label( root , bd=2 , text="")
self.lbl.pack()
self.lbl1 = tk.Label( root , bd=2 , text="Players :- ")
self.lbl1.pack()
self.mylist = []
self.lbl2 = tk.Label( root, bd=2 , text="")
self.lbl2.pack()
def addPlayer(self):
if len(self.e2.get()) == 0 or (len(self.e2.get().strip()) == 0) :
self.lbl.config(text='Please enter valid player name!!')
return
else:
self.lbl.config(text='')
self.mylist.append(self.e2.get())
self.lbl2.config(text='\n'.join(map(str, self.mylist)))
self.v.set('')
root = tk.Tk()
root.title("Mac Application")
root.resizable(width=False, height=False)
root.geometry('600x400')
obj = ListExample()
tk.mainloop()
Now you can run this app from your command line and just enter players name and players list will be automatic updated.
1 Comment(s)