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
:)