#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    14
    Rep Power
    0

    Messagebox,dialogbox?


    Hi I'm using python3
    I want to know if you could help me with this problem:
    This program asks the user to enter how many tickets to purchase the range is 1 to 5 tickets.

    I want to make it so the user can only enter 1,2,3,4 or 5
    and get an error message if they don't.

    Thanks for any help.




    Code:
    from tkinter import *
    ticket=2
    ####
    
    def calculatetotals():
         mytik=float(tik .get())
         total=mytik*ticket
         labelresults =Label(aGUI,text= "You purchased:$%.2f \n" \
                                                           %  total).grid(row=7,column=2)
                                                       
         label3 =Label(aGUI,text= "Number Of Tckets: %.f"  \
                                              %  mytik).grid(row=6,column=1)
                                                         
         button2=Button(aGUI,text="Click Here To Draw Your Tickets!"\
                                                                                   ,fg='blue',bg='white'\
                                                                                           ,command=exit)\
                                                                                 .grid(row=8,column=1)
         
         
         return
    
    ####
    
    aGUI=Tk()
    aGUI.geometry('400x200+100+200')
    aGUI.title("LOTTO")
    ####
    tik=StringVar()
    # ###                                                                            
    
    label1=Label(aGUI,text="WELCOME TO LOTTO."\
                                 ,fg='blue').grid(row=0,column=2)
    
    label2=Label(aGUI,text="Tickets Are $2.00 Each."\
                                       ,fg='red').grid(row=1,column=1)
    
    label3=Label(aGUI,text="How Many Would You Like?"\
                                           ,fg='black').grid(row=2,column=1)
    ####                                                                                
    mytik = Entry(aGUI,textvariable=tik).grid(row=2,column=2)\
    ####                                                                                       
                    
    button1=Button(aGUI,text="Your Purchase\n",fg='blue'\
                                                          ,command=calculatetotals)\
                                                                   .grid(row=6,column=1)
    ####                                                                                    
    
    aGUI.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    It might surprise you to learn that all the variables you've named as labeln and buttonn assigned to the values of expressions ending in .grid() actually receive the output of the grid method, which is None. Those assignments are useless. Save the Labels and Buttons when you later want to change their attributes.

    Avoid ugly troublesome backslash line continuations.

    Ticket price appears more than once in your program. I changed the extra occurrence I found to string formatting, simplifying program modification when the ticket price increases.
    Code:
    from tkinter import *
    
    ticket_price = 2.00
    
    def calculatetotals():
        valid = set('12345')
        ticket_entry = tik.get()
        if ticket_entry not in valid:
            print('We permit values in {}.'.format(valid))
        else:
            mytik=float(ticket_entry)
            total=mytik*ticket_price
            Label(aGUI,text= "You purchased:$%.2f \n" % total).grid(row=7,column=2)
            Label(aGUI,text= "Number Of Tckets: %.f" % mytik).grid(row=6,column=1)
            Button(aGUI,text="Click Here To Draw Your Tickets!"
                   ,fg='blue',bg='white'
                   ,command=exit).grid(row=8,column=1)
    
    aGUI=Tk()
    aGUI.geometry('400x200+100+200')
    aGUI.title("LOTTO")
    
    tik=StringVar()
    
    label1=Label(aGUI,text="WELCOME TO LOTTO.",fg='blue')
    label1.grid(row=0,column=2)
    
    label2=Label(aGUI,
                 text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
    label2.grid(row=1,column=1)
    
    label3=Label(aGUI,text="How Many Would You Like?",fg='black')
    label3.grid(row=2,column=1)
    
    mytik = Entry(aGUI,textvariable=tik)
    mytik.grid(row=2,column=2)
    
    button1=Button(aGUI,text="Your Purchase\n",fg='blue',command=calculatetotals)
    button1.grid(row=6,column=1)
    
    aGUI.mainloop()
    Last edited by b49P23TIvg; February 6th, 2014 at 06:17 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    14
    Rep Power
    0
    ok Thanks for your help
    I thought .grid was just a place where widgets will be placed on the main window.
    I'm not sure, I understand your answer. Is button1 different than say buttona? isn't button1 one variable, with one value.

    I also tried to create a (if)(else) but I did not know the (valid)part.
    Nice to know that valid is in python
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    You need to study basic python concepts.
    Work through the tutorial http://docs.python.org/3/tutorial/index.html.

    I'm not going to write your lotto machinery any more.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    14
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You need to study basic python concepts.
    Work through the tutorial http://docs.python.org/3/tutorial/index.html.

    I'm not going to write your lotto machinery any more.
    ok fair enough not to completely write this stuff, but you should know that all the code you say you wrote was only revised. The original actually did the same as your revised code, just by someone who cant write programs So this is why I ask for help in this forum. I don't expect any help unless I have exhausted what little ability I have.


    Thank for your revisions though.
    and the link tuts
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    You're missing the point. You say:

    "Nice to know that valid is in python"

    which to me indicates that you don't know that valid is a plain old variable name.
    valid = set('12345')


    You say:

    ".grid was just a place where widgets will be placed on the main window."

    which to me indicates that you don't know that functions and methods always return values, or that the syntax
    Code:
    Label(aGUI,text="WELCOME TO LOTTO.",fg='blue').grid(row=0,column=2)
    invokes a function or method. (method)



    In other words, I'll work with you to learn python fundamentals.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    14
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You're missing the point. You say:

    "Nice to know that valid is in python"

    which to me indicates that you don't know that valid is a plain old variable name.
    valid = set('12345')


    You say:

    ".grid was just a place where widgets will be placed on the main window."

    which to me indicates that you don't know that functions and methods always return values, or that the syntax
    Code:
    Label(aGUI,text="WELCOME TO LOTTO.",fg='blue').grid(row=0,column=2)
    invokes a function or method. (method)



    In other words, I'll work with you to learn python fundamentals.
    Your rite, I don't know any of this python I am just trying to get info and to get it on the screen ,but tkinter is a pain and when I need to mix it with python, I get lost on the first line. then I can't think all of it through. I don't even know how to break these actions into smaller problems.
    but I thank you for the help
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    tkinter has been about impossible for me to learn, and I understood python fairly well before I started using it. And I've read some of the tcl documentation as well as the 3 X11 books giving me gui concepts. Drawing pictures on Apple ][ was trivial, drawing and music with the IBM PC BASICA were trivial (even if it sounded horrid)
    play"cdefgo5abc"
    or something like that played a scale. Now when I want my computer to play a pitch I start audacity, pull up a piano I recorded, adjust the frequency, and play the new tone. Sounds great---but non-trivial, and not a full scale.
    Last edited by b49P23TIvg; February 8th, 2014 at 12:16 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    14
    Rep Power
    0
    I think i may need a really nice book on python3+ or tkinter
    let me know, if you know any specific like this.
    It seem alot of info is written in this part of Idle >>> yet I can't do anything in that part I keep having to start over I don't know what this >>> area is in comparison to the blank page where I can write programs and not have to start over.

IMN logo majestic logo threadwatch logo seochat tools logo