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

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

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

  1. # import
  2. import tkinter as tk
  3. # tkinterWindow
  4. top = tk.Tk()
  5. top.geometry("300x400")
  6. top.title("Basic Calculator")
  7. top.configure(bg="black")
  8. top.resizable(False, False)
  9. # Functions
  10. expression = ""
  11. def input_number(number):
  12. global expression
  13. expression = expression + str(number)
  14. # buttons
  15. button1 = tk.Button(top,text="1",
  16. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  17. input_number(1))
  18. button1.pack(side="left")
  19. button1.place(y=275, x=20)
  20. button2 = tk.Button(top,text="2",
  21. pady=10,highlightbackground="#000000",fg="white", width=5,command=lambda:
  22. input_number(2))
  23. button2.pack(side="left")
  24. button2.place(y=275, x=90)
  25. button3 = tk.Button(top,text="3",
  26. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  27. input_number(3))
  28. button3.pack(side="left")
  29. button3.place(y=275, x=160)
  30. button4 = tk.Button(top,text="+",
  31. pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda:
  32. input_number("+"))
  33. button4.pack(side="left")
  34. button4.place(y=275, x=230)
  35. button5 = tk.Button(top,text="4",
  36. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  37. input_number(4))
  38. button5.pack(side="left")
  39. button5.place(y=225, x=20)
  40. button6 = tk.Button(top,text="5",
  41. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  42. input_number(5))
  43. button6.pack(side="left")
  44. button6.place(y=225, x=90)
  45. button7 = tk.Button(top,text="6",
  46. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  47. input_number(6))
  48. button7.pack(side="left")
  49. button7.place(y=225, x=160)
  50. button8 = tk.Button(top,text="-",
  51. pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda:
  52. input_number("-"))
  53. button8.pack(side="left")
  54. button8.place(y=225, x=230)
  55. button9 = tk.Button(top,text="7",
  56. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  57. input_number(7))
  58. button9.pack(side="left")
  59. button9.place(y=175, x=20)
  60. button10 = tk.Button(top,text="8",
  61. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  62. input_number(8))
  63. button10.pack(side="left")
  64. button10.place(y=175, x=90)
  65. button11 = tk.Button(top,text="9",
  66. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  67. input_number(0))
  68. button11.pack(side="left")
  69. button11.place(y=175, x=160)
  70. button12 = tk.Button(top,text="x",
  71. pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda:
  72. input_number("*"))
  73. button12.pack(side="left")
  74. button12.place(y=175, x=230)
  75. button13 = tk.Button(top,text="AC",
  76. pady=10,highlightbackground="#000000",fg="#20B245", width=5,
  77. command=lambda: clear())
  78. button13.pack(side="left")
  79. button13.place(y=125, x=20)
  80. button14 = tk.Button(top,text="+/-",
  81. pady=10,highlightbackground="#000000",fg="#20B245", width=5)
  82. button14.pack(side="left")
  83. button14.place(y=125, x=90)
  84. button15 = tk.Button(top,text="%",
  85. pady=10,highlightbackground="#000000",fg="#20B245", width=5)
  86. button15.pack(side="left")
  87. button15.place(y=125, x=160)
  88. button16 = tk.Button(top,text="÷",
  89. pady=10,highlightbackground="#000000",fg="red", width=5, command=lambda:
  90. input_number("/"))
  91. button16.pack(side="left")
  92. button16.place(y=125, x=230)
  93. button17 = tk.Button(top,text="0",
  94. pady=10,highlightbackground="#000000",fg="white", width=5, command=lambda:
  95. input_number(0))
  96. button17.pack(side="left")
  97. button17.place(y=325, x=90)
  98. button18 = tk.Button(top,text=",",
  99. pady=10,highlightbackground="#000000",fg="white", width=5)
  100. button18.pack(side="left")
  101. button18.place(y=325, x=160)
  102. button19 = tk.Button(top,text="=",
  103. pady=10,highlightbackground="#000000",fg="red", width=5)
  104. button19.pack(side="left")
  105. button19.place(y=325, x=230)
  106. # Label
  107. label1 = tk.Label(top, highlightbackground="white", width=50, height=6)
  108. label1.pack(side="top")
  109. label1.place(y=0)
  110. top.mainloop()

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

ssgvzors

ssgvzors1#

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

dkqlctbz

dkqlctbz2#

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

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

相关问题