Andre Bormans
Andre Bormans

Reputation: 41

The buttons of the tkinter window turns white when opening other windows

I am fairly new to programming with Python. When I output my code, I get the desired output

When I click on an other window (for example chrome) and then return to the output, the desired output becomes this

This is my code:

#import
import tkinter as tk

#tkinterWindow
top = tk.Tk()
top.geometry("300x400")
top.title("Basic Calculator")
top.configure(bg="black")
top.resizable(False, False)

#Functions
expression = ""

def input_number(number):
  global expression
  expression = expression + str(number)

#buttons
button1 = tk.Button(top,text="1", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(1))
button1.pack(side="left")
button1.place(y=275, x=20)
button2 = tk.Button(top,text="2", 
pady=10,highlightbackground="#000000",fg="white", width=5,command=lambda: 
input_number(2))
button2.pack(side="left")
button2.place(y=275, x=90)
button3 = tk.Button(top,text="3", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(3))
button3.pack(side="left")
button3.place(y=275, x=160)
button4 = tk.Button(top,text="+", 
pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda: 
input_number("+"))
button4.pack(side="left")
button4.place(y=275, x=230)
button5 = tk.Button(top,text="4", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(4))
button5.pack(side="left")
button5.place(y=225, x=20)
button6 = tk.Button(top,text="5", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(5))
button6.pack(side="left")
button6.place(y=225, x=90)
button7 = tk.Button(top,text="6", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(6))
button7.pack(side="left")
button7.place(y=225, x=160)
button8 = tk.Button(top,text="-", 
pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda: 
input_number("-"))
button8.pack(side="left")
button8.place(y=225, x=230)
button9 = tk.Button(top,text="7", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(7))
button9.pack(side="left")
button9.place(y=175, x=20)
button10 = tk.Button(top,text="8", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(8))
button10.pack(side="left")
button10.place(y=175, x=90)
button11 = tk.Button(top,text="9", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(0))
button11.pack(side="left")
button11.place(y=175, x=160)
button12 = tk.Button(top,text="x", 
pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda: 
input_number("*"))
button12.pack(side="left")
button12.place(y=175, x=230)
button13 = tk.Button(top,text="AC", 
pady=10,highlightbackground="#000000",fg="#20B245", width=5, 
command=lambda: clear())
button13.pack(side="left")
button13.place(y=125, x=20)
button14 = tk.Button(top,text="+/-", 
pady=10,highlightbackground="#000000",fg="#20B245", width=5)
button14.pack(side="left")
button14.place(y=125, x=90)
button15 = tk.Button(top,text="%", 
pady=10,highlightbackground="#000000",fg="#20B245", width=5)
button15.pack(side="left")
button15.place(y=125, x=160)
button16 = tk.Button(top,text="÷", 
pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda: 
input_number("/"))
button16.pack(side="left")
button16.place(y=125, x=230)
button17 = tk.Button(top,text="0", 
pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda: 
input_number(0))
button17.pack(side="left")
button17.place(y=325, x=90)
button18 = tk.Button(top,text=",", 
pady=10,highlightbackground="#000000",fg="white", width=5)
button18.pack(side="left")
button18.place(y=325, x=160)
button19 = tk.Button(top,text="=", 
pady=10,highlightbackground="#000000",fg="red", width=5)
button19.pack(side="left")
button19.place(y=325, x=230)

#Label
label1 = tk.Label(top, highlightbackground="white", width=50, height=6)
label1.pack(side="top")
label1.place(y=0)

top.mainloop() 

I am programming on a macbook (macOS High Sierra) and in visual studio code. Why are my buttons changing to the color white?

Upvotes: 2

Views: 628

Answers (3)

toyota Supra
toyota Supra

Reputation: 4543

Add namespace:

from tkmacosx import Button, Label

Upvotes: 1

Mudit Dagar
Mudit Dagar

Reputation: 81

button9 = tk.Button(top,text="7", 
pady=10,bg="blue",fg="black", width=5, command=lambda: 
input_number(7))
button9.pack(side="left")

use bg and fg at the place of highlightbackground and then try I think it should work.

Upvotes: 1

Lincoln Jones
Lincoln Jones

Reputation: 11

It may have something to do with your computer. You said you have a mac-book and sometimes I have noticed if I am programming with Mac or doing anything with Mac it changes the look of the window when I am not using it. I think mac os has a program running to do that to save power or something. Sorry i was not much of a help.

Upvotes: 0

Related Questions