Hi I am having a problem with the "nextz" part "Click Here to see the new winning number." I want to kill this button after it shows the winning number. but it prints another number every time I click the button.
how can i click once show once.
Thanks for any help.

from tkinter import*
import sys, random, tkinter 
ticket_price = 2
ticket_entry = 0
nextx = 0
L=0
g=0
f=0
i=0

stop=None
total = 0
end=None

def calculatetotals():
    valid = set('12345')
    ticket_entry = tik.get()
    if ticket_entry not in valid:
        print('We permit values in {}.'.format(valid))
        label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
    else:
        label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
        mytik=int(ticket_entry)
        total = mytik*ticket_price

        Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=2)      
        Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)        
        Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white',command = nextx).grid(row=8,column=1)

aApp=Tk()                                                                                          
aApp.geometry('350x200+150+250')
aApp.title("LOTTO")

tik=StringVar()

label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
label1.grid(row=0,column=2)

label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
label2.grid(row=1,column=1)

label3=Label(aApp,text="How Many Would You Like?",fg='black')
label3.grid(row=2,column=1)

mytik = Entry(aApp,textvariable=tik)
mytik.grid(row=2,column=2)

button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue',command=calculatetotals)
button1.grid(row=6,column=1)

def nextx():

             Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black',command = nextz).grid(row=8,column=1)

             ticket_entry = tik.get()
             i = 0           
             while i < int(ticket_entry):

               L = list(range(1,60))
               random.shuffle(L)

               g = L[:5]
               g.sort()
               f = L[5:6]

               drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])

               label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)

               i+=1

app=tkinter.Tk()
app.geometry('600x400+75+75')
app.title(string="     NUMBERS      ")    

Button(app, text="Quit", command=app.quit).pack()

def  nextz():

                              L = list(range(1,60))
                              random.shuffle(L)
                              g = L[:5]
                              g.sort()
                              f = L[5:6]

                              drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])

                              label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)
                              sys.exit("This is an example of an error message")

                              return 

aBpp=tkinter.Tk()
aBpp.geometry('200x100+705+705')
aBpp.title(string="Winning Number")

Button(aBpp, text="Quit", command=aBpp.quit).pack()

tkinter.mainloop()

app.mainloop()

aApp.mainloop()

Your program has many issues. The main problem is that you have many instances of the Tk() class, which leads to unknown results. Use Toplevel to open a new GUI. This is code I have lying around that shows how to use grid_forget. The buttons are stored in a dictionary, but for one button that is not necessary. You could also use grid_remove or destroy to remove/delete the button.

from Tkinter import *
from functools import partial

class ButtonsTest:
   def __init__(self):
      self.top = Tk()
      self.top.title('Buttons Test')
      self.top_frame = Frame(self.top, width =400, height=400)
      self.button_dic = {}
      self.buttons()
      self.top_frame.grid(row=0, column=1)

      Button(self.top_frame, text='Exit', 
             command=self.top.quit).grid(row=10,column=1, columnspan=5)

      self.top.mainloop()

   ##-------------------------------------------------------------------         
   def buttons(self):
      b_row=1
      b_col=0
      for but_num in range(1, 11):
         b = Button(self.top_frame, text = str(but_num), 
                    command=partial(self.cb_handler, but_num))
         b.grid(row=b_row, column=b_col)
         self.button_dic[but_num] = b

         b_col += 1
         if b_col > 4:
            b_col = 0
            b_row += 1

   ##----------------------------------------------------------------
   def cb_handler( self, cb_number ):
      print "\ncb_handler", cb_number
      self.button_dic[cb_number].grid_forget()

##===================================================================
BT=ButtonsTest()

Thanks i will run this and see.
I sure my program has more issue than what you mentioned but I just want it to run and have results I don't know programing at all.
so I am just following the error messages for now.
It gives me an errorwhere the # is below
print "\ncb_handler"#, cb_number

It gives me an errorwhere the # is below
print "\ncb_handler"#, cb_number

"It gives me an error" is not nearly enough info. Are you using Python 3.x? There is a difference between print statements for 2.x vs. 3.x, so convert to a 3.X print statement if you are.

Edited 2 Years Ago by woooee

ok I think i fixed It and made it so you can run it from the command line using C:\Python33 keep.py

The problem next is The Compare button does nothing yet
I dont know an aproache to "compare" the numbers generated to the number that is the win number. Should I use "r" "w" files
to store 1,2,3,4or5 sets of numbers.
I will just say I purched 1 ticket and I will 'compare' it to the winning drawn number.
Should I use files?
I don't know how to proceed.
Thanks for any help.

from tkinter import*
import sys, random, tkinter 
ticket_price = 2
i=0
total = 0

def calculatetotals():
    valid = set('12345')
    ticket_entry = tik.get()
    if ticket_entry not in valid:
        print('We permit values in {}.'.format(valid))
        label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
    else:
        label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
        mytik=int(ticket_entry)
        total = mytik*ticket_price

        Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=1)      
        Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)        
        Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white',command = nextx).grid(row=8,column=1)

aApp=Tk()                                                                                          
aApp.geometry('580x170+200+270')
aApp.title("LOTTO")

tik=StringVar()

label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
label1.grid(row=0,column=2)

label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
label2.grid(row=1,column=1)

label3=Label(aApp,text="How Many Would You Like?",fg='black')
label3.grid(row=2,column=1)

mytik = Entry(aApp,textvariable=tik)
mytik.grid(row=2,column=2)

button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue',command=calculatetotals)
button1.grid(row=6,column=1)

def nextx():

             Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black',command = nextz).grid(row=8,column=1)
             ticket_entry = tik.get()
             i = 0           
             while i < int(ticket_entry):

               L = list(range(1,60))
               random.shuffle(L)
               g = L[:5]
               g.sort()
               f = L[5:6]
               drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])
               label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)
               i+=1

app=tkinter.Tk()
app.geometry('600x400+75+75')
app.title(string="     NUMBERS      ")    

Button(app, text="Quit", command=app.quit).pack()
def  nextz():

                              Button(aApp,text='Push   again to  <Quit> : To see if you match Push <Compare> In The New Winning Number Window, ',fg='yellow',bg='black',command = quit).grid(row=8,column=1)                                  
                              L = list(range(1,60))
                              random.shuffle(L)
                              g = L[:5]
                              g.sort()
                              f = L[5:6]

                              drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])

                              aBpp=tkinter.Tk()
                              aBpp.geometry('200x100+705+705')
                              aBpp.title(string="Winning Number")
                              label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)

                              Button(aBpp, text="Compare", command=aBpp.quit).pack()

                              return

tkinter.mainloop()
app.mainloop()
aApp.mainloop()

using python33
I can run the above code without any errors.
I added this to output to a file in the (while).Only the text_file parts

 while i < int(ticket_entry):

               L = list(range(1,60))
               random.shuffle(L)
               g = L[:5]
               g.sort()
               f = L[5:6]
               drawing = '  '.join( ['  '.join(str(G)  for  G  in g),' -',str(f[0])])
               label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)

               text_file = open("C:\output.txt","a")
               text_file.write('\n')
               text_file.write(drawing)
               text_file.close()
               i+=1

I don't know how to make the leading 0's

OUTPUT of the file:
10 12 16 17 46 - 28
4 14 36 52 57 - 55
3 14 25 47 59 - 3
7 39 49 51 58 - 36
22 24 32 42 45 - 41

DESIRED output to the file:
leading zeros on single digit numbers

10 12 16 17 46 - 28
04 14 36 52 57 - 55
03 14 25 47 59 - 03
07 39 49 51 58 - 36
22 24 32 42 45 - 41

THANKS for any help here

So here is the program so far.
I don't know the difference between python 2 and 3 print statments.

As far as i know this should all work.
It is sloppy I am just trying to get it to work and try to fix things as I go.

from tkinter import*
    import sys, random, tkinter 
    ticket_price = 2
    i=0
    total = 0
    #outFile=0
    def calculatetotals():
        valid = set('12345')
        ticket_entry = tik.get()
        if ticket_entry not in valid:
            print('We permit values in {}.'.format(valid))
            label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
        else:
            label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
            mytik=int(ticket_entry)
            total = mytik*ticket_price

            Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=1)      
            Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)        
            Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white',command = nextx).grid(row=8,column=1)

    aApp=Tk()                                                                                          
    aApp.geometry('580x170+200+270')
    aApp.title("LOTTO")

    tik=StringVar()

    label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
    label1.grid(row=0,column=2)

    label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
    label2.grid(row=1,column=1)

    label3=Label(aApp,text="How Many Would You Like?",fg='black')
    label3.grid(row=2,column=1)

    mytik = Entry(aApp,textvariable=tik)
    mytik.grid(row=2,column=2)

    button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue',command=calculatetotals)
    button1.grid(row=6,column=1)

    def nextx():

                 Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black',command = nextz).grid(row=8,column=1)
                 ticket_entry = tik.get()
                 i = 0           
                 while i < int(ticket_entry):

                   L = list(range(1,60))
                   random.shuffle(L)
                   g = L[:5]
                   g.sort()
                   f = L[5:6]
                   drawing = '  '.join( ['  '.join(str(G)  for  G  in g),' -',str(f[0])])
                   label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)

                   text_file = open("C:\output.txt","a")
                   text_file.write('\n')
                   text_file.write(drawing)
                   text_file.close()
                   i+=1




    app=tkinter.Tk()
    app.geometry('600x400+75+75')
    app.title(string="     NUMBERS      ")    

    Button(app, text="Quit", command=app.quit).pack()
    def  nextz():

                                  Button(aApp,text='Push   again to  <Quit> : To see if you match Push <Compare> In The New Winning Number Window, ',fg='yellow',bg='black',command = quit).grid(row=8,column=1)                                  
                                  L = list(range(1,60))
                                  random.shuffle(L)
                                  g = L[:5]
                                  g.sort()
                                  f = L[5:6]

                                  drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])


                                  aBpp=tkinter.Tk()
                                  aBpp.geometry('200x100+705+705')
                                  aBpp.title(string="Winning Number")
                                  label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)

                                  Button(aBpp, text="Compare", command=aBpp.quit).pack()

                                  return


    tkinter.mainloop()
    app.mainloop()
    aApp.mainloop()

Edited 2 Years Ago by trade19: added explaination

This article has been dead for over six months. Start a new discussion instead.