Does anyone know the code to click a button widget and get the value of that button to show up on an entry screen. I'll show you my code.

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createEdit()
        self.createView()
        self.createHelp()
        self.createQuit()
        self.createOnOff()
        self.createDelete()
        self.createClear()
        self.createSeven()
        self.createEight()
        self.createNine()
        self.createDivide()
        self.createFour()
        self.createFive()
        self.createSix()
        self.createTimes()
        self.createOne()
        self.createTwo()
        self.createThree()
        self.createMinus()
        self.createZero()
        self.createNegative()
        self.createDecimal()
        self.createPlus()
        self.createEquals()





## Buttons at top of window, adjust to suit proper format
## Use page 43 in the Tkinter book for pop down button




    def createEdit(self):
        self.mb = Menubutton(self, text="Edit", relief=FLAT, font=("Arial", 8))
        self.mb.grid(row=0, column=0)
        self.mb.menu = Menu(self.mb, tearoff=1)
        self.mb["menu"] = self.mb.menu
        self.copyVar = IntVar()
        self.pasteVar = IntVar()
        self.mb.menu.add_checkbutton(label="Copy Ctrl+C", variable=self.copyVar)
        self.mb.menu.add_checkbutton(label="Paste Ctrl+V", variable=self.pasteVar)

    def createView(self):
        self.mb = Menubutton(self, text="View", relief=FLAT, font=("Arial", 8))
        self.mb.grid(row=0, column=1)
        self.mb.menu = Menu(self.mb, tearoff=1)
        self.mb["menu"] = self.mb.menu
        self.standardVar = IntVar()
        self.digitGroupingVar = IntVar()
        self.mb.menu.add_checkbutton(label="Standard", variable=self.standardVar)
        self.mb.menu.add_checkbutton(label="Digit Grouping", variable=self.digitGroupingVar)

    def createHelp(self):
        self.mb = Menubutton(self, text="Help", relief=FLAT, font=("Arial", 8))
        self.mb.grid(row=0, column=2)
        self.mb.menu = Menu(self.mb, tearoff=1)
        self.mb["menu"] = self.mb.menu
        self.helpTopicsVar = IntVar()
        self.aboutExtremeCalculatorVar = IntVar()
        self.mb.menu.add_checkbutton(label="Help Topics", variable=self.helpTopicsVar)
        self.mb.menu.add_checkbutton(label="About Extreme Calculator", variable=self.aboutExtremeCalculatorVar)

    def createQuit(self):
        self.mb = Menubutton(self, text="Quit", relief=FLAT, font=("Arial", 8))
        self.mb.grid(row=0, column=3)
        self.mb.menu = Menu(self.mb, tearoff=0)
        self.mb["menu"] = self.mb.menu
        self.exitVar = IntVar()
        self.mb.menu.add_command(label="Exit", command=self.quit)




## Screen




    entry = tk.Entry(width=35, bg="yellow")
    entry.grid(row=1, column=0)





## First Row of buttons





    def createOnOff(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="On/Off", font=("Arial", 12), bg="white", fg="magenta", cursor="crosshair")
        self.Button.grid(row=1, column=0, sticky=N+E+S+W)

    def createDelete(self):
        top=self.winfo_toplevel()
        top.rowconfigure(1, weight=1)
        top.columnconfigure(1, weight=1)
        self.rowconfigure(1, weight=1)
        self.columnconfigure(1, weight=1)
        self.Button = Button ( self, text="Delete", font=("Arial", 12), bg="white", fg="magenta", cursor="crosshair")
        self.Button.grid(row=1, column=1, sticky=N+E+S+W)

    def createClear(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="Clear", font=("Arial", 12), bg="white", fg="magenta", cursor="crosshair")
        self.Button.grid(row=1, column=2, sticky=N+E+S+W)

    def createDivide(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    ÷    ", font=("Arial", 12), bg="white", fg="red", cursor="crosshair")
        self.Button.grid(row=1, column=3, sticky=N+E+S+W)





## Second Row of buttons





    def createSeven(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=0)
        top.columnconfigure(0, weight=0)
        self.rowconfigure(0, weight=0)
        self.columnconfigure(0, weight=0)
        self.Button = Button ( self, text="    7    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=2, column=0, sticky=N+E+S+W)

    def createEight(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    8    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=2, column=1, sticky=N+E+S+W)

    def createNine(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    9    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=2, column=2, sticky=N+E+S+W)

    def createTimes(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    *    ", font=("Arial", 12), bg="white", fg="red", cursor="crosshair")
        self.Button.grid(row=2, column=3, sticky=N+E+S+W)





## Third Row of buttons





    def createFour(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    4    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=3, column=0, sticky=N+E+S+W)

    def createFive(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    5    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=3, column=1, sticky=N+E+S+W)

    def createSix(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    6    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=3, column=2, sticky=N+E+S+W)

    def createMinus(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    -    ", font=("Arial", 12), bg="white", fg="red", cursor="crosshair")
        self.Button.grid(row=3, column=3, sticky=N+E+S+W)





## Forth Row of buttons





    def createOne(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    1    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=4, column=0, sticky=N+E+S+W)

    def createTwo(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    2    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=4, column=1, sticky=N+E+S+W)

    def createThree(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    3    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=4, column=2, sticky=N+E+S+W)

    def createPlus(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    +    ", font=("Arial", 12), bg="white", fg="red", cursor="crosshair")
        self.Button.grid(row=4, column=3, sticky=N+E+S+W)





## Fifth Row of buttons





    def createZero(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    0    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=5, column=0, sticky=N+E+S+W)

    def createNegative(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    +/-    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=5, column=1, sticky=N+E+S+W)

    def createDecimal(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    .    ", font=("Arial", 12), bg="white", fg="blue", cursor="crosshair")
        self.Button.grid(row=5, column=2, sticky=N+E+S+W)

    def createEquals(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.Button = Button ( self, text="    =    ", font=("Arial", 12), bg="white", fg="black", cursor="crosshair")
        self.Button.grid(row=5, column=3, sticky=N+E+S+W)




## Last part




        
app = Application()
app.master.title("Extreme Calculator") # This will be the name of the window
app.mainloop()

If you know the code reply ASAP and much thanks

:)

Recommended Answers

All 5 Replies

You need to bind each button to an event handler and then append the Entry box's text in each handler according to what button was pressed.

alright man, but could you give me an example of the code to kind of head in the right direction!!

:)

Following is an example where I already have defined a list of text strings (self.btnList) to be displayed on buttons. When a button is selected (actually, the sequence is ButtonRelease), the button text is assigned to instance attribute "value" and the app is destroyed. In your case, you want to "set" the value of the string variable (Tkinter.StringVar) that was assigned to an entry widget textvariable.

def singleChoice(self, parent):
        # Create button frame and buttons
        self.buttonFrame = Tkinter.LabelFrame(parent, text="Available options", labelanchor="n")
        self.buttonFrame.config(relief="ridge", bd=3)
        self.buttonFrame.grid()
        self.value = None
        self.buttonList = []
        j=-1
        for i, t in enumerate(self.btnList):
            btn = Tkinter.Button(self.buttonFrame,
                                 text=t,
                                 anchor='center',
                                 bd=3,
                                 bg='#ffffff000',
                                 fg="#000000fff",
                                 activebackground = "#000000fff",
                                 activeforeground = "#ffffff000",
                                 font=textFont3,
                                 padx='1.0m',
                                 pady='1.0m',
                                 relief='raised',
                                 state='normal',
                                 width=self.btnWidth)
            self.buttonList.append((btn, t))
            if not i%self.columns:
                j += 1
            btn.grid(row=j, column=i%self.columns)
            def handler(event, i=i):
                return self.__buttonHandler(event, i)
            btn.bind(sequence="<ButtonRelease-1>", func=handler)
    
    def __buttonHandler(self, event, btnNumber):
        setattr(self, 'value', self.buttonList[btnNumber][1])
        self.destroy()

that doesn't do anything and i put it into my code and it still doesn't do anything. please tell me how to do it from my code and tell me!!

thx

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.