【问题标题】:The buttons of the tkinter window turns white when opening other windows打开其他窗口时 tkinter 窗口的按钮变为白色
【发布时间】:2021-09-18 22:26:26
【问题描述】:

我对使用 Python 编程相当陌生。当我输出我的代码时,我得到了desired output

当我点击另一个窗口(例如chrome)然后返回输出时,所需的输出变为this

这是我的代码:

#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 代码进行编程。 为什么我的按钮变成白色?

【问题讨论】:

  • 我认为这是由于highlightbackground。但是,我不确定
  • 使用bg 代替highlightbackground
  • 尝试使用 ttk.Buttons 和 ttk.Style, ttk.Style maps

标签: python user-interface tkinter button


【解决方案1】:

这可能与您的计算机有关。你说你有一本 mac-book,有时我注意到如果我用 Mac 编程或用 Mac 做任何事情,当我不使用它时它会改变窗口的外观。我认为mac os有一个程序正在运行以节省电力或其他东西。抱歉,我帮不上什么忙。

【讨论】:

    【解决方案2】:
    button9 = tk.Button(top,text="7", 
    pady=10,bg="blue",fg="black", width=5, command=lambda: 
    input_number(7))
    button9.pack(side="left")
    

    在highlightbackground的地方使用bg和fg,然后试试我觉得应该可以。

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 2014-08-07
      • 2017-06-14
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多