Using tkinter library in Python - python

I have two python files gui.py, student.py. i have imported tkinter, i will ask the user to enter their name, id, email, and address, Using tkinter widget i will display all in a list. How to do this using class ?
this is gui.py
import tkinter
import student
class MyGUI:
def __init__(self):
self.__students = []
# Create the main window widget
self.main_window = tkinter.Tk()
self.name_f = tkinter.Frame(self.main_window)
self.id_f = tkinter.Frame(self.main_window)
self.email_f = tkinter.Frame(self.main_window)
self.addy_f = tkinter.Frame(self.main_window)
self.buttons_f = tkinter.Frame(self.main_window)
# Create a Label and Entry widget for each item in
# the Student class
self.name_l = tkinter.Label(self.name_f, text='Name: ')
self.name_e = tkinter.Entry(self.name_f, width=10)
self.id_l = tkinter.Label(self.id_f, text='ID: ')
self.id_e = tkinter.Entry(self.id_f, width=10)
self.email_l = tkinter.Label(self.email_f, text='Email: ')
self.email_e = tkinter.Entry(self.email_f, width=10)
self.addy_l = tkinter.Label(self.addy_f, text='Address: ')
self.addy_e = tkinter.Entry(self.addy_f, width=10)
self.add_b = tkinter.Button(self.buttons_f, text='Add Current Data', command=self.add)
self.display_b = tkinter.Button(self.buttons_f, text='List All', command=self.display)
self.quit_b = tkinter.Button(self.buttons_f, text='Quit', command=self.main_window.destroy)
self.name_l.pack(side='left')
self.name_e.pack(side='left')
self.id_l.pack(side='left')
self.id_e.pack(side='left')
self.email_l.pack(side='left')
self.email_e.pack(side='left')
self.addy_l.pack(side='left')
self.addy_e.pack(side='left')
self.add_b.pack(side='left')
self.display_b.pack(side='left')
self.quit_b.pack(side='left')
self.name_f.pack()
self.id_f.pack()
self.email_f.pack()
self.addy_f.pack()
self.buttons_f.pack()
#Enter the tkinter main loop
tkinter.mainloop()
def add(self):
# we will do this in class
pass
def display(self):
# we will do this in class
pass
# Create an instance of the MyGUI class
my_gui = MyGUI()
and this is the student.py
class Student:
# this a comment
# most languages define attributes sep
# Declare String name
def setName(self, n):
self.name = n
def setId(self, i):
self.sid = i
def setEmail(self, e):
# check to see if e has an # sign
self.email = e
def setAddy(self, a):
self.addy = a
def getName(self):
return self.name
def getId(self):
return self.sid
def getEmail(self):
return self.email
def getAddy(self):
return self.addy
def printInfo(self):
info = "Name: "
info += self.name
info += '\nID: '
info += self.sid
info += '\nEmail: '
info += self.email
info += '\nAddress: '
info += self.addy
info += '\n'
return info

Asking for code solutions to something you should be learning yourself probably isn't a good idea.
Just look up the documentation for TKinter here: https://docs.python.org/3/library/tkinter.html
As a note, you may want to consider opening separate windows when displaying the information. When I first learned TKinter, I first practiced by printing all my data to the console with the print command before trying to force them into tables.
Also, consider fleshing out the GUI and make it look like a table. Give each label a fixed length and each box a fixed length, this will make it look better.
A good example of this in action is on this amazing website for learning TKinter: http://www.tkdocs.com/tutorial/firstexample.html
Just scroll down until you find the Python implementation of the code :)

Related

How do I carry over a variable from one class to another?

I'm trying to program a application that carries over user inputs from one page to the other where the pages are separated by classes. A problem I'm having is that the array output on page 2 isn't updating. Basically the output is just [] which is just the starting initialized variable. I've tried some solutions I've found on stack overflow such as calling it a global variable again at the init bit of the PageTwo class as well as trying to use PageOne.overall_data and using self.overall_data but the problem persists although there weren't any errors. This is a part of the code before I tried anything. I tried to cut out the irrelevant bits but I'm not sure if I cut out too much, feedback is much appreciated. Thanks for reading!
import tkinter as tk
import tkinter_nav as tknav
from tkinter import ttk
import numpy as np
class App(tknav.Wrapper):
def __init__(self):
tknav.Wrapper.__init__(
self,
pages=[PageOne, PageTwo],
start_state={'previous_page': None}
)
self.geometry('450x450')
self.show_page('page_one')
class PageOne(tknav.Page):
def __init__(self, parent):
tknav.Page.__init__(self, parent, 'page_one')
player_details = []
global overall_data
overall_data = []
my_notebook = ttk.Notebook(self)
my_notebook.pack(pady = 15)
my_notebook.pack(fill="both", expand=True)
my_frame1 = tk.Frame(my_notebook, width = "500", height = "500")
def submit(): #called every time inputs are made to be appended to player_details then overall_data
player_details = []
global overall_data
player_details.append(name.get())
player_details.append(health.get())
player_details.append(ac.get())
player_details.append(initiative.get())
overall_data.append(player_details)
overall_data = sorted(overall_data, key = lambda x:x[3])
print(str(overall_data))
class PageTwo(tknav.Page): #second page of the application
def __init__(self, parent):
tknav.Page.__init__(self, parent, 'page_two')
tk.Label(
self,
text='Page Two'
).pack()
tk.Button(
self,
text='Navigate to Page One',
command=lambda: self.__navigate(),
).pack()
line21 = tk.Label(self, text = str(overall_data), font = ('Times New Roman', 12))
line21.place(x = 30, y = 30, width = 100, height = 25)
def __navigate(self):
print('navigating to page one')
self.navigate('page_one')
if __name__ == '__main__':
App().mainloop()
You can put your data at the class level in PageOne like this:
class PageOne(tknav.Page):
overall_data = []
and use it everywhere like this:
PageOne.overall_data.append(player_details)
Since you have used tkinter_nav, you can use its provided app_state to share data between pages:
class App(tknav.Wrapper):
def __init__(self):
tknav.Wrapper.__init__(
self,
pages=[PageOne, PageTwo],
start_state={'previous_page': None, 'overall_data': []} # added 'overall_data'
)
self.geometry('450x450')
self.show_page('page_one')
Then you can access this shared data in each page by:
self.app_state['overall_data']

python 3.5 pass object from one class to another

I am trying to figure out how to pass data from one class into another. My knowledge of python is very limited and the code I am using has been taken from examples on this site.
I am trying to pass the User name from "UserNamePage" class into "WelcomePage" class. Can someone please show me how to achieve this. I will be adding more pages and I will need to pass data between the different pages
Below is the full code - as mentioned above most of this code has come from other examples and I am using these examples to learn from.
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import datetime
import re
def Chk_String(mystring):
Allowed_Chars = re.compile('[a-zA-Z_-]+$')
return Allowed_Chars.match(mystring)
def FnChkLogin(Page):
booAllFieldsCorrect = False;
myFName = Page.FName.get()
myFName = myFName.replace(" ", "")
myLName = Page.LName.get()
myLName = myLName.replace(" ", "")
if myFName == "":
messagebox.showinfo('Login Ifo is Missing', "Please type in your First Name")
elif not Chk_String(myFName):
messagebox.showinfo('First Name Error:', "Please only use Leter or - or _")
elif myLName == "":
messagebox.showinfo('Login Info is Missing', "Please type in your Last Name")
elif not Chk_String(myLName):
messagebox.showinfo('Last Name Error:', "Please only use Leter or - or _")
else:
booAllFieldsCorrect = True;
if booAllFieldsCorrect == True:
app.geometry("400x200")
app.title("Welcome Screen")
PageController.show_frame(app,"WelcomePage")
def FnAddButton(Page,Name,Label,Width,X,Y,FnAction):
Name = ttk.Button (Page, text=Label,width=int(Width),command=FnAction)
Name.place(x=int(X),y=int(Y))
class PageController(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top",fill="both",expand="True")
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames={}
for F in (UserNamePage,WelcomePage):
page_name = F.__name__
frame = F(container,self)
self.frames[page_name] = frame
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame("UserNamePage")
def show_frame(self,page_name):
frame= self.frames[page_name]
frame.tkraise()
class UserNamePage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
lblFName = Label(self,text="First Name ",relief=GROOVE,width=12,anchor=E).place(x=50,y=50)
lblLName = Label(self,text="Last Name ",relief=GROOVE,width=12,anchor=E).place(x=50,y=75)
self.FName = StringVar()
inputFName = Entry(self,textvariable=self.FName,width=25).place(x=142,y=50)
self.LName = StringVar()
inputLName = Entry(self,textvariable=self.LName,width=25).place(x=142,y=75)
cmdContinue = ttk.Button (self, text='Continue',width=9,command=lambda:FnChkLogin(self)).place(x=320,y=70)
class WelcomePage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
UserNamePageData = UserNamePage(parent,controller)
UserFName = str(UserNamePageData.FName)
UserLName = str(UserNamePageData.LName)
strWelcome = "Welcome " + UserFName + " " + UserLName
lblWelcome = Label(self,text=strWelcome,relief=FLAT,width=50,anchor=W).place(x=25,y=25)
if __name__ == "__main__":
app = PageController()
app.geometry("400x200")
app.title("User Page")
app.eval('tk::PlaceWindow %s center' % app.winfo_pathname(app.winfo_id()))
app.mainloop()
In the method show_frame(self,page_name) in class PageController, add the following lines
if page_name == 'WelcomePage':
self.frames['WelcomePage'].UserFName = self.frames['UserNamePage'].FName.get()
self.frames['WelcomePage'].UserLName = self.frames['UserNamePage'].LName.get()
and remove the two lines UserFName = str(UserNamePageData.FName)and UserLName = str(UserNamePageData.LName).
Explanation: it must be done in a place that has references to both frames (i.e. class PageController).

What's the most efficient way to pass a parameter between classes in Python?

I am currently building a user based system, where I have a class for a login screen in pythons tkinter. Once the user information is correct it will initiate a class for the menu. I'm using an SQL database which python reads and checks user information with. I'm going to need the username of the user to be passed to the menu class so the class knows what information to display on that menu.
from tkinter import *
class Login:
def __init__(self, master):
self.master = master
self.label = Label(self.master, text = "enter name")
self.entry = Entry(self.master)
self.button = Button(self.master, text = "submit", command = self.loadMenu)
self.label.grid(row = 0, column = 0)
self.entry.grid(row = 0, column = 1)
self.button.grid(row = 1, column = 0, columnspan = 2)
self.name = self.entry.get()
def loadMenu(self):
self.menu = Toplevel(self.master)
self.app = Menu(self.menu)
class Menu:
def __init__(self, master):
self.master = master
self.label = Label(self.master, text = "dave")
self.label.grid(row = 0, column = 0)
def main():
root = Tk()
run = Login(root)
root.mainloop()
if __name__ == '__main__':
main()
In the above example code, what method could be used to pass the variable 'self.name' to class menu so that it can be displayed in a label? I am trying not to use a global variable. Many Thanks.
Your class Menu, needs a method to set the name. e.g.:
class myMenu:
def __init__(self,...):
....
def set_Me(self,some_name):
self.some_name = some_name
# or
self.label.setName(some_name)
in the main program you call:
self.app.set_Me(self.name)
The above is pseudo code. But you should get the idea.
This technique you can use to pass variables to any class at any time.
If you need to pass variable only once:
class my_class:
def __init__(self,my_variable):
self.my_variable = my_variable

Having multiple-page GUI in Python with several buttons

I am currently trying to implement a GUI for a task at work. I have found some open source code (see below) where we make a GUI having 3 pages where one can go between the pages using next/previous buttons. If you run the code you'll see what each button and so on is indended to do.
However, when you run the code and click the "count++" button, the overall count increases by one and not the count for the individual page (e.g. being on page 1 and clicking the count++ 4 times, still makes the count for page 2 or 3, 4 as well, and not zero). The same problem occurs when I try to update the text in each of the textboxes on each page (supposed to be number of clicks), as it won't update. I am not sure how to actually get to the textframe for each individual page.
Any suggestions on where to go from here? In the long run I would like to have scroll-down menus where the selections will be put onto each individual text-frame.
Thanks,
import ttk
from Tkinter import *
import tkMessageBox
class Wizard(object, ttk.Notebook):
def __init__(self, master=None, **kw):
npages = kw.pop('npages', 3)
kw['style'] = 'Wizard.TNotebook'
ttk.Style(master).layout('Wizard.TNotebook.Tab', '')
ttk.Notebook.__init__(self, master, **kw)
self._children = {}
self.click_count = 0
self.txt_var = "Default"
for page in range(npages):
self.add_empty_page()
self.current = 0
self._wizard_buttons()
def _wizard_buttons(self):
"""Place wizard buttons in the pages."""
for indx, child in self._children.iteritems():
btnframe = ttk.Frame(child)
btnframe.pack(side='left', fill='x', padx=6, pady=4)
txtframe = ttk.Frame(child)
txtframe.pack(side='right', fill='x', padx=6, pady=4)
nextbtn = ttk.Button(btnframe, text="Next", command=self.next_page)
nextbtn.pack(side='top', padx=6)
countbtn = ttk.Button(txtframe, text="Count++..", command=self.update_click)
countbtn.grid(column=0,row=0)
txtBox = Text(txtframe,width = 50, height = 20, wrap = WORD)
txtBox.grid(column=1,row=0)
txtBox.insert(0.0, self.txt_var)
rstbtn = ttk.Button(btnframe, text="Reset count!", command=self.reset_count)
rstbtn.pack(side='top', padx=6)
if indx != 0:
prevbtn = ttk.Button(btnframe, text="Previous",
command=self.prev_page)
prevbtn.pack(side='right', anchor='e', padx=6)
if indx == len(self._children) - 1:
nextbtn.configure(text="Finish", command=self.close)
def next_page(self):
self.current += 1
def prev_page(self):
self.current -= 1
def close(self):
self.master.destroy()
def add_empty_page(self):
child = ttk.Frame(self)
self._children[len(self._children)] = child
self.add(child)
def add_page_body(self, body):
body.pack(side='top', fill='both', padx=6, pady=12)
def page_container(self, page_num):
if page_num in self._children:
return self._children[page_num]
else:
raise KeyError("Invalid page: %s" % page_num)
def _get_current(self):
return self._current
def _set_current(self, curr):
if curr not in self._children:
raise KeyError("Invalid page: %s" % curr)
self._current = curr
self.select(self._children[self._current])
current = property(_get_current, _set_current)
def update_click(self):
self.click_count += 1
message = "You have clicked %s times now!" % str(self.click_count)
tkMessageBox.showinfo("monkeybar", message)
self.txt_var = "Number of clicks: %s" % str(self.click_count) #this will not change the text in the textbox!
def reset_count(self):
message = "Count is now 0."
#ctypes.windll.user32.MessageBoxA(0, message, "monkeybar", 1)
tkMessageBox.showinfo("monkeybar", message)
self.click_count = 0
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def demo():
root = Tk()
nbrpages = 7
wizard = Wizard(npages=nbrpages)
wizard.master.minsize(400, 350)
wizard.master.title("test of GUI")
pages = range(nbrpages)
for p in pages:
pages[p] = ttk.Label(wizard.page_container(p), text='Page %s'%str(p+1))
wizard.add_page_body(pages[p])
wizard.pack(fill='both', expand=True)
root.mainloop()
if __name__ == "__main__":
demo()
Your update_click method operate on the attribute click_count of your wizard. If you want different count, you could either create a class for your pages and thus, each object would manage its own count, or manage several counter, for instance in a list, the same way you handle your _children list.
For the former case, you can create a page class inheriting ttk.Frame with the body of your _wizard_buttons loop as constructor. In the later case, you could try something like this
class Wizard(object, ttk.Notebook):
def __init__(self, master=None, **kw):
[...]
#replace self.click_count = 0 with
self.click_counters = [0 for i in range(npages)]
def update_click(self):
self.click_counters[self.current] += 1
# and so on...
Regarding the Text widget update, you can not handle it though a variable, it works with Entry(one line text field) but not on Text (multiline, rich text field). If you continue with Text, the usual recipe to what you seems to want is
text.delete(1.0, END)
text.insert(END, content)

Python Tkinter frame inside frame limitation or user error?

I've been building an app to track stock prices. The user should see a window with an entry widget and a button that creates a new frame with a label and a button. The label is the stock price and symbol, the button is a delete button, and should hide that frame if clicked.
I've re-written this program 4 times now, and it's been a great learning experience, but what I've learned is that I can't have the "mini-frames" being called from methods part of the main GUI class - this funks up the delete buttons, and updates the value behind frame.pack_forget() so it only deletes the last item ever.
I've moved my mini-frame widgets down into the class for the actual stock values. I've packed them (what I assume to be correct) but they don't show up. They also don't error out, which isn't very helpful. Here's my code, although I've omitted a lot of the functional parts to show what is happening with my frames. Keep in mind I need to keep it so that I can call my updater (self.update_stock_value) with a .after method against myapp.myContainer.
Is there a better way to do this?? Thanks in advance, my head hurts.
import re
import time
import urllib
from Tkinter import *
import threading
from thread import *
runningThreads = 0
# each object will be added to the gui parent frame
class MyApp(object):
def __init__(self, parent):
self.myParent = parent
self.myContainer = Canvas(parent)
self.myContainer.pack()
self.create_widgets()
# METHOD initiates basic GUI widgets
def create_widgets(self):
root.title("Stocker")
self.widgetFrame = Frame(self.myContainer)
self.widgetFrame.pack()
self.input = Entry(self.widgetFrame)
self.input.focus_set()
self.input.pack()
self.submitButton = Button(self.widgetFrame, command = self.onButtonClick)
self.submitButton.configure(text = "Add new stock")
self.submitButton.pack(fill = "x")
# METHOD called by each stock object
# returns the "symbol" in the entry widget
# clears the entry widget
def get_input_value(self):
var = self.input.get()
self.input.delete(0, END)
return var
# METHOD called when button is clicked
# starts new thread with instance of "Stock" class
def onButtonClick(self):
global runningThreads # shhhhhh im sorry just let it happen
runningThreads += 1 # count the threads open
threading.Thread(target = self.init_stock,).start() # force a tuple
if runningThreads == 1:
print runningThreads, "thread alive"
else:
print runningThreads, "threads alive"
def init_stock(self):
new = Stock()
class Stock(object):
def __init__(self):
# variable for the stock symbol
symb = self.stock_symbol()
# lets make a GUI
self.frame = Frame(myapp.myContainer)
self.frame.pack
# give the frame a label to update
self.testLabel = Label(self.frame)
self.testLabel.configure(text = self.update_stock_label(symb))
self.testLabel.pack(side = LEFT)
# create delete button to kill entire thread
self.killButton = Button(self.frame, command = self.kill_thread)
self.killButton.configure(text = "Delete")
self.killButton.pack(side = RIGHT)
# create stock label
# call updater
def kill_thread(self):
global runningThreads
runningThreads -= 1
self.stockFrame.pack_forget() # hide the frame
self.thread.exit() # kill the thread
def update_stock_label(self, symb):
self.testLabel.configure(text = str(symb) + str(get_quote(symb)))
myapp.myContainer.after(10000, self.update_stock_label(symb))
def stock_symbol(self):
symb = myapp.get_input_value()
print symb
# The most important part!
def get_quote(symbol):
try:
# go to google
base_url = "http://finance.google.com/finance?q="
# read the source code
content = urllib.urlopen(base_url + str(symbol)).read()
# set regex target
target = re.search('id="ref_\d*_l".*?>(.*?)<', content)
# if found, return.
if target:
print "found target"
quote = target.group(1)
print quote
else:
quote = "Not Found: "
return quote
# handling if no network connection
except IOError:
print "no network detected"
root = Tk()
root.geometry("280x200")
myapp = MyApp(root)
root.mainloop()
Your code won't run because of numerous errors, but this line is definitely not doing what you think it is doing:
self.frame.pack
For you to call the pack function you must include (), eg:
self.frame.pack()
You ask if your code is the best way to do this. I think you're on the right track, but I would change a few things. Here's how I would structure the code. This just creates the "miniframes", it doesn't do anything else:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.entry = tk.Entry(self)
self.submit = tk.Button(self, text="Submit", command=self.on_submit)
self.entry.pack(side="top", fill="x")
self.submit.pack(side="top")
def on_submit(self):
symbol = self.entry.get()
stock = Stock(self, symbol)
stock.pack(side="top", fill="x")
class Stock(tk.Frame):
def __init__(self, parent, symbol):
tk.Frame.__init__(self, parent)
self.symbol = tk.Label(self, text=symbol + ":")
self.value = tk.Label(self, text="123.45")
self.symbol.pack(side="left", fill="both")
self.value.pack(side="left", fill="both")
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()

Categories

Resources