python - Tkinter Calculator Bug -


i making simple python tk calculator, there bug: calculator won't work unless push 1 of buttons before numbers put in when diplays error says "enter numeric values!" after calculate input numbers if open , input numbers won't work

here code:

from tkinter import * import math  def calculate():     try:         num1 = float(enter1.get())         num2 = float(enter2.get())         result = num1 * num2         label3.config(text=str(result))     except valueerror:         label3.config(text='enter numeric values!', fg="white")  def calculate2():     try:         num1 = float(enter1.get())         num2 = float(enter2.get())         result = num1 / num2         label3.config(text=str(result))     except valueerror:         label3.config(text='enter numeric values!', fg="white")  def calculate3():     try:         num1 = float(enter1.get())         num2 = float(enter2.get())         result = num1 + num2         label3.config(text=str(result))    except valueerror:         label3.config(text='enter numeric values!',fg="white")  def calculate4():     try:         num1 = float(enter1.get())         num2 = float(enter2.get())         result = num1 - num2         label3.config(text=str(result))     except valueerror:         label3.config(text='enter numeric values!',fg="white")  def calculate5():     try:         num1 = float(enter1.get())         result = num1**2         label3.config(text=str(result))     except valueerror:         label3.config(text='enter numeric values!',fg="white")  def calculate6():     try:         num1 = float(enter1.get())         result = math.sqrt(num1)         label3.config(text=str(result))     except valueerror:         label3.config(text='enter numeric values!',fg="white")   root = tk() root.configure(background='black') root.wm_title("calc")  label1 = label(root, text='first number:',bg="black", fg="white") label1.grid(row=0, column=0,columnspan=2) enter1 = entry(root, bg='white') enter1.grid(row=1, column=0,columnspan=2)  label2 = label(root, text='second number:',bg="black", fg="white") label2.grid(row=2, column=0,columnspan=2) enter2 = entry(root, bg='white') enter2.grid(row=3, column=0, columnspan=2)  btn1 = button(root, text='-multiply-', command=calculate,                                       bg="  black",activebackground="green", fg="white") btn1.grid(row=4, column=0) btn2 = button(root, text='-divide-', command=calculate2,  bg="black",activebackground="orange", fg="white") btn2.grid(row=5, column=0) btn3 = button(root, text='-add-', command=calculate3, bg="black",activebackground="purple", fg="white") btn3.grid(row=5, column=1) btn4 = button(root, text='-subtract-', command=calculate4, bg="black",activebackground="red", fg="white") btn4.grid(row=4, column=1) btn5 = button(root, text='square (only first #)', command=calculate5, bg="black",activebackground="cyan", fg="white") btn5.grid(row=6, column=0, columnspan=2) btn6 = button(root, text='square root (only first #)', command=calculate6, bg="black",activebackground="yellow", fg="white") btn6.grid(row=7, column=0, columnspan=2,) label3 = label(root, bg="black") label3.grid(row=8, column=0, columnspan=2)  enter1.focus() enter1.bind('<return>', func=lambda e:enter2.focus_set()) root.mainloop() 

your code works without problem, issue text color black. why "works" before displaying error, because option sets fg option white, , in next changes keeps same text color has been configured.

to solve this, set fg option "white" when create label widget:

label3 = label(root, bg="black", fg="white") 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -