【发布时间】: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