我真的不知道怎么做按钮绑定,因为我之前从未尝试过,但我确实有一个按钮绑定代码,我的问题是,我无法将工作按钮绑定代码合并到我的计算器代码中它使用一个类。我需要做些什么来完成这项工作?
这是我有的按钮绑定代码:
{ 'choices' : [{'Form_id' : 11065,'Fields' : [{'ReflectionField_id' : 283860}],'Name' : 'B'}] }
这是我的实际计算器代码:
from functools import partial
from tkinter import *
root = Tk()
def key(event):
print ("pressed", repr(event.char))
if event.char.isdigit():
append_digit(event.char)
elif event.char in ('\x08', '\x7f'):
backspace()
def callback(event):
frame.focus_set()
print ("clicked at", event.x, event.y)
frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()
current = IntVar(0)
label = Label(frame, textvariable=current)
label.pack()
def button_callback(i):
print ("clicked button {}".format(i))
append_digit(i)
def append_digit(digit):
current.set(current.get() * 10 + int(digit))
def backspace():
current.set(current.get() // 10)
for i in '1234567890':
Button(frame, text=i, command=partial(button_callback, i)).pack()
Button(frame, text='C', command=backspace).pack()
frame.focus_set()
root.mainloop()
答案 0 :(得分:0)
你的按钮绑定(回调)都是正确的。
问题在于您的self.results
条目 -
self.results = Entry(display, textvariable=numbers, width=31, fg="snow", bg="snow", font="Verdana")
您已将字体颜色和背景颜色设置为相同颜色,因此即使数字显示在您的计算器中,它们也不可见。将字体颜色(fg)更改为其他内容,例如黑色等 -
self.results = Entry(display, textvariable=numbers, width=31, fg="black", bg="snow", font="Verdana")
对于实际需要,在问题中不清楚,即绑定键盘键。您放置的按钮/操作员绑定脚本的主要部分是 -
frame.bind("<Key>", key)
并且
def key(event):
print ("pressed", repr(event.char))
if event.char.isdigit():
append_digit(event.char)
elif event.char in ('\x08', '\x7f'):
backspace()
你可以简单地将它们添加到你的班级(以及其他一些小的改进) -
from tkinter import *
from functools import partial
from tkinter import messagebox
root = Tk()
root.title("Calculator")
root.geometry("317x145")
menubar = Menu(root)
class Calculator(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
master.bind("<Key>", self.key)
display = Frame(root, bd=0, width=1000, height=1000, relief=SUNKEN)
buttons = Frame(root, bd=0, width=7, height=1, relief=GROOVE)
display.grid(column=0, row=0, padx=0, pady=0)
buttons.grid(column=0, row=1, padx=1)
numbers = StringVar()
self.results = Entry(display, textvariable=numbers, width=31, fg="black", bg="snow", font="Verdana")
self.results.pack()
self.results.grid(column=0, row=0)
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for i in range(9):
n=numbers[i]
Button(buttons, bg="snow", text=n, width=7, height=1, command=self.show_up(n), relief=RAISED).grid(row=i%3, column=i//3)
Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
Clear.grid(padx=2, pady=2, column=3, row=0)
Equals = Button(buttons, bg="snow", text="=", width=7, height=1, command=self.equals, relief=RAISED)
Equals.grid(padx=2, pady=2, column=4, row=3)
All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
All_clear.grid(padx=2, pady=2, column=4, row=0)
Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
Bracket_one.grid(padx=2, pady=2, column=2, row=3)
Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
Bracket_two.grid(padx=2, pady=2, column=3, row=3)
Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
Zero.grid(padx=2, pady=2, column=0, row=3)
Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
Decimal_point.grid(padx=2, pady=2, column=1, row=3)
Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
Multiplication.grid(padx=2, pady=2, column=3, row=1)
Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
Division.grid(padx=2, pady=2, column=4, row=1)
Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
Addition.grid(padx=2, pady=2, column=3, row=2)
Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
Subtraction.grid(padx=2, pady=2, column=4, row=2)
def key(self, event):
if event.char.isdigit():
self.results.insert(END, event.char)
elif event.char in ('\x08', '\x7f'):
self.clear()
def show_up(self, x):
return lambda: self.results.insert(END, x)
def equals(self):
try:
result = eval(self.results.get())
except:
result = "Error"
self.all_clear()
self.results.insert(0, result)
def zero(self):
self.results.insert(END, "0")
def bracket_one(self):
self.results.insert(END, "(")
def bracket_two(self):
self.results.insert(END, ")")
def all_clear(self):
self.results.delete(0, END)
def clear(self):
self.results.delete(len(self.results.get()) - 1)
def multiplication(self):
self.results.insert(END, "*")
def division(self):
self.results.insert(END, "/")
def addition(self):
self.results.insert(END, "+")
def subtraction(self):
self.results.insert(END, "-")
def decimal_point(self):
self.results.insert(END, ".")
if __name__ == '__main__':
Calculator(root)
root.config(menu=menubar)
root.mainloop()