LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2017, 12:34 PM   #1
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 119

Rep: Reputation: Disabled
python Tkinter - writing a multi page application


Hi,
I'm totally self taught in python 3 with only slight previous exposure to OOP. Developing the examples from the beginners book which taught me Tkinter i have written the following multi screen application, it's a contrived example of typical screens in an order processing system.
Code:
from tkinter import *

class SelectOrder(Frame):

    screen=0
    
    def __init__(self,master):
        super(SelectOrder,self).__init__(master)
        self.grid()
        self.define_widgets()


    def define_widgets(self):

        temp=Label(self,text="Order Entry")
        temp.grid(row=0,column=1,columnspan=2,sticky=W)

        temp=Label(self,text="Order number :")
        temp.grid(row=2,column=0,sticky=E)

        self.order_number=StringVar()
        temp=Entry(self,textvariable=self.order_number)
        temp.grid(row=2,column=1,columnspan=3,sticky=W)

        temp=Button(self,text="Exit",command=self.leave)
        temp.grid(row=8,column=0,sticky=W)

        temp=Button(self,text="Next",command=self.btn_next)
        temp.grid(row=8,column=3,sticky=W)


    def leave(self):
        quit()

    def btn_next(self):
        SelectOrder.screen=1
        self.quit()

class CustomerAddress(Frame):

    screen=1
    
    def __init__(self,master):
        super(CustomerAddress,self).__init__(master)
        self.grid()
        self.define_widgets()


    def define_widgets(self):

        temp=Label(self,text="Order Entry")
        temp.grid(row=0,column=1,columnspan=2,sticky=W)

        temp=Label(self,text="Order number :")
        temp.grid(row=2,column=0,sticky=E)

        self.order_number=Label(self,text="How do i str(order_number)")
        self.order_number.grid(row=2,column=1,sticky=W)
        
        temp=Label(self,text="Address :")
        temp.grid(row=4,column=0,sticky=E)

        self.address1=StringVar()
        temp=Entry(self,textvariable=self.address1)
        temp.grid(row=4,column=1,columnspan=3,sticky=W)

        self.address2=StringVar()
        temp=Entry(self,textvariable=self.address2)
        temp.grid(row=5,column=1,columnspan=3,sticky=W)

        temp=Button(self,text="Exit",command=self.leave)
        temp.grid(row=8,column=0,sticky=W)

        temp=Button(self,text="Prev",command=self.btn_prev)
        temp.grid(row=8,column=2,sticky=W)

        temp=Button(self,text="Next",command=self.btn_next)
        temp.grid(row=8,column=3,sticky=W)


    def leave(self):
        quit()

    def btn_next(self):
        CustomerAddress.screen=2
        self.quit()

    def btn_prev(self):
        CustomerAddress.screen=0
        self.quit()

class OrderHeader(Frame):

    screen=2
    
    def __init__(self,master):
        super(OrderHeader,self).__init__(master)
        self.grid()
        self.define_widgets()


    def define_widgets(self):

        temp=Label(self,text="Order Header")
        temp.grid(row=0,column=1,columnspan=2,sticky=W)

        temp=Label(self,text="Order number :")
        temp.grid(row=2,column=0,sticky=E)

        self.order_number=Label(self,text="How do i str(order_number)")
        self.order_number.grid(row=2,column=1,sticky=W)
        
        temp=Label(self,text="Delviery :")
        temp.grid(row=4,column=0,sticky=E)

        self.delivery=StringVar()
        temp=Entry(self,textvariable=self.delivery)
        temp.grid(row=4,column=1,columnspan=3,sticky=W)

        temp=Label(self,text="Order Type :")
        temp.grid(row=5,column=0,sticky=E)

        self.order_type=StringVar()
        temp=Entry(self,textvariable=self.order_type)
        temp.grid(row=5,column=1,columnspan=3,sticky=W)

        temp=Button(self,text="Exit",command=self.leave)
        temp.grid(row=8,column=0,sticky=W)

        temp=Button(self,text="Prev",command=self.btn_prev)
        temp.grid(row=8,column=2,sticky=W)

        temp=Button(self,text="Done",command=self.btn_done)
        temp.grid(row=8,column=3,sticky=W)


    def leave(self):
        quit()

    def btn_done(self):
        OrderHeader.screen=-1
        self.quit()

    def btn_prev(self):
        OrderHeader.screen=1
        self.quit()

#
# main

root=Tk()
root.title("multi screen application")
root.geometry("600x300")

window=SelectOrder(root)
screen=window.screen

address1=None
address2=None
order_type=None
delivery=None

while True:
    root.mainloop()

    if screen==0:
        order_number=window.order_number.get()
    elif screen==1:
        address1=window.address1.get()
        address2=window.address2.get()
    elif screen==2:
        order_type=window.order_type.get()
        delivery=window.delivery.get()

    screen=window.screen

    if screen<0:
        print("write data")
        break
        
    #
    # this has to be in a try: as when you click X (window close) rather
    # than exit i get a double destruction problem 
    try:
        window.destroy()
    except TclError:
        quit()

    if screen==0:
        window=SelectOrder(root)
        window.order_number.set(order_number)
    elif screen==1:
        window=CustomerAddress(root)
        window.order_number["text"]=order_number
        if address1!=None:
            window.address1.set(address1)
        if address2!=None:
            window.address2.set(address2)    
    else:
        window=OrderHeader(root)
        window.order_number["text"]=order_number
        if order_type!=None:
            window.order_type.set(order_type)
        if delivery!=None:
            window.delivery.set(delivery)
Is this the correct way to achieve a multi page application?

the try/except block at line 185
Code:
    try:
        window.destroy()
    except TclError:
        quit()
is acceptable, but as i develop this solution i'm having to wrap all window.some_identifier code in try/except blocks and the code is becoming unreadable.
The problem is that i need to probe Tkinter when i exit from root.mainloop() to check if i reached there by an application button being clicked and issuing self.quit() or the kill-window-button on the title bar has been pressed.

can i add some code like this
Code:
...
while True:
    root.mainloop()

    if root.is_killed():
         quit()
# *OR*
    if window.is_killed():
         quit()

...
I can't help thinking that what seemed to be a clean readable solution isn't the best or even correct way to achieve a multi page application.

Any advice or python pointers happily received.

Last edited by rhubarbdog; 11-27-2017 at 12:36 PM.
 
Old 12-08-2017, 05:28 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,528
Blog Entries: 6

Rep: Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437
As no one else has stepped up, I did a quick search for "python tkinter" which produced many hits, including the project home page:

Python Tkinter

I have not used Tkinter so cannot offer any personal tips, but it looks like well explored territory on DUckDuckGo!

Good luck!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python - tkinter application auto start on boot up - problems awn Linux - Newbie 2 05-03-2010 09:28 PM
How do I import Tkinter into Python mugglemage Linux - Software 5 07-02-2008 09:47 AM
Python and Tkinter 1337 Twinkie Fedora 2 08-03-2004 12:35 PM
Python: Tkinter. Chu Programming 0 11-10-2003 02:56 AM
Python and Tkinter Error Gerardoj Programming 1 11-08-2003 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration