打开其他窗口时,tkinter窗口的按钮变为白色

rfbsl7qr  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(328)

我对python编程相当陌生。当我输出代码时,我得到了所需的输出
当我单击另一个窗口(例如chrome)然后返回到输出时,所需的输出变成
这是我的代码:


# 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()

我正在用macbook(macos high sierra)和visual studio代码编程。为什么我的按钮会变成白色?

ssgvzors

ssgvzors1#

这可能与你的电脑有关。你说你有一本mac书,有时我注意到如果我用mac编程或用mac做任何事情,当我不使用它时,它会改变窗口的外观。我认为mac os有一个程序可以运行,这样做是为了省电或其他什么。对不起,我帮不了什么忙。

dkqlctbz

dkqlctbz2#

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

在highlightbackground的位置使用背景和前景,然后尝试我认为应该可以。

相关问题