我不确定为什么没有显示“开始计算”消息

时间:2019-04-16 10:05:59

标签: python python-3.x tkinter

我正在编写一个计算器应用程序,并使用Tkinter和python 3.7。我目前陷入困境,因为当我尝试运行它时,除屏幕上不显示“开始计算...”外,其他所有东西都起作用。有人能帮忙吗?

我尝试调用最后使用的函数,但这没有用。我不确定还有什么尝试。

from tkinter import *

def btn(numbers):
    global operator
    operator = operator + str(numbers)

root = Tk()
root.title("Calculator")

operator = ''
txt_input = StringVar(value='Start Calculating...')

#========================Screen=======================
Display = Entry(root, fg='white', bg='white', 
    justify='right', bd=28, textvariable=txt_input)
Display.grid(columnspan=5, sticky='NSEW')

#=======================Row1==========================
b7 = Button(root, padx=30, pady=15, bd=8, fg='black', text='7', highlightbackground='white', highlightthickness=0.0001).grid(row=1, column=0, sticky='NSEW')
b8 = Button(root, padx=30, pady=15, bd=8, fg='black', text='8', highlightbackground='white', highlightthickness=0.0001).grid(row=1, column=1, sticky='NSEW')
b9 = Button(root, padx=30, pady=15, bd=8, fg='black', text='9', highlightbackground='white', highlightthickness=0.0001).grid(row=1, column=2, sticky='NSEW')
clear = Button(root, padx=30, pady=15, bd=8, fg='black', text='C', highlightbackground='green', highlightthickness=0.0001).grid(row=1, column=3, sticky='NSEW')

#=======================Row2==========================
b4 = Button(root, padx=30, pady=15, bd=8, fg='black', text='4', highlightbackground='white', highlightthickness=0.0001).grid(row=2, column=0, sticky='NSEW')
b5 = Button(root, padx=30, pady=15, bd=8, fg='black', text='5', highlightbackground='white', highlightthickness=0.0001).grid(row=2, column=1, sticky='NSEW')
b6 = Button(root, padx=30, pady=15, bd=8, fg='black', text='6', highlightbackground='white', highlightthickness=0.0001).grid(row=2, column=2, sticky='NSEW')
plus = Button(root, padx=30, pady=15, bd=8, fg='black', text='+', highlightbackground='orange', highlightthickness=0.0001).grid(row=2, column=3, sticky='NSEW')

#=======================Row3==========================
b1 = Button(root, padx=30, pady=15, bd=8, fg='black', text='1', highlightbackground='white', highlightthickness=0.0001).grid(row=3, column=0, sticky='NSEW')
b2 = Button(root, padx=30, pady=15, bd=8, fg='black', text='2', highlightbackground='white', highlightthickness=0.0001).grid(row=3, column=1, sticky='NSEW')
b3 = Button(root, padx=30, pady=15, bd=8, fg='black', text='3', highlightbackground='white', highlightthickness=0.0001).grid(row=3, column=2, sticky='NSEW')
minus = Button(root, padx=30, pady=15, bd=8, fg='black', text='-', highlightbackground='orange', highlightthickness=0.0001).grid(row=3, column=3, sticky='NSEW')

#=======================Row4==========================
b0 = Button(root, padx=30, pady=15, bd=8, fg='black', text='0', highlightbackground='white', highlightthickness=0.0001).grid(row=4, column=0, sticky='NSEW')
dot = Button(root, padx=30, pady=15, bd=8, fg='black', text='.', highlightbackground='orange', highlightthickness=0.0001).grid(row=4, column=1, sticky='NSEW')
div = Button(root, padx=30, pady=15, bd=8, fg='black', text='/', highlightbackground='orange', highlightthickness=0.0001).grid(row=4, column=2, sticky='NSEW')
times = Button(root, padx=30, pady=15, bd=8, fg='black', text='x', highlightbackground='orange', highlightthickness=0.0001).grid(row=4, column=3, sticky='NSEW')

#=======================Row5==========================
equals = Button(root, padx=95, pady=15, bd=8, fg='black', text='=', highlightbackground='green', highlightthickness=0.0001).grid(row=5, columnspan=2)
bracket1 = Button(root, padx=35, pady=15, bd=8, fg='black', text='(', highlightbackground='orange', highlightthickness=0.0001).grid(row=5, column=2)
bracket2 = Button(root, padx=38, pady=15, bd=8, fg='black', text=')', highlightbackground='orange', highlightthickness=0.0001).grid(row=5, column=3)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

您的问题是前景(文本)和背景颜色均为白色,因此无论如何您都看不到任何文本。

请注意: 要在条目中放置值,如果不想将其与变量关联,也可以使用insert条目方法:

Display.insert(0, 'Start Calculating...')Display.insert(0, txt_input.get()) 0是要在条目文本中插入指定文本的位置的索引(位置),即,将文本插入第0个位置-起始位置。