2013-12-11 112 views
0

我已經提供了一些使用回調來允許輸入框從按鈕中獲取值的代碼。問題是,我怎樣才能在輸入框中回調兩個連接值。如果用戶點擊1然後點擊7,則顯示值17。從Tkinter中的按鈕輸入框中輸入多個條目

from tkinter import * 
win = Tk() 
win.geometry("210x125") 

def set_text(text): 
    e.insert(0,text) 
    return 

e = Entry(win,width=35) 
e.grid(columnspan=4) 

seven_button = Button(win,width=6,text="7",command=lambda:set_text("7")).grid(row=1, column=0) 
eight_button = Button(win,width=6,text="8",command=lambda:set_text("8")).grid(row=1, column=1) 
nine_button = Button(win,width=6,text="9",command=lambda:set_text("9")).grid(row=1, column=2) 
div_button = Button(win,width=6,text="÷",command=lambda:set_text("÷")).grid(row=1, column=3) 

four_button = Button(win,width=6,text="4",command=lambda:set_text("4")).grid(row=2, column=0) 
five_button = Button(win,width=6,text="5",command=lambda:set_text("5")).grid(row=2, column=1) 
six_button = Button(win,width=6,text="6",command=lambda:set_text("6")).grid(row=2, column=2) 
multiply_button = Button(win,width=6,text="x",command=lambda:set_text("x")).grid(row=2, column=3) 

one_button = Button(win,width=6,text="1",command=lambda:set_text("1")).grid(row=3, column=0) 
two_button = Button(win,width=6,text="2",command=lambda:set_text("2")).grid(row=3, column=1) 
three_button = Button(win,width=6,text="3",command=lambda:set_text("3")).grid(row=3, column=2) 
minus_button = Button(win,width=6,text="-",command=lambda:set_text("-")).grid(row=3, column=3) 

zero_button = Button(win,width=14,text="0",command=lambda:set_text("0")).grid(columnspan=2) 
point_button = Button(win,width=6,text=".",command=lambda:set_text(".")).grid(row=4, column=2) 
plus_button = Button(win,width=6,text="+",command=lambda:set_text("+")).grid(row=4, column=3) 

win.mainloop() 

回答

0

如果你想完整的功能,我可以建議添加一個等於按鈕,將計算入口小部件中的一切嗎?

import Tkinter as tk 
win = tk.Tk() 
win.geometry("300x300") 


def compute(entry): 
string_to_compute = tk.StringVar() 
string_to_compute = entry.get() 
entry.delete(0, tk.END) 
if '+' in string_to_compute: 
    l = string_to_compute.split('+')[0] 
    r = string_to_compute.split('+')[1] 
    entry.insert(0, float(l) + float(r)) 
elif '-' in string_to_compute: 
    l = string_to_compute.split('-')[0] 
    r = string_to_compute.split('-')[1] 
    entry.insert(0, float(l) - float(r)) 
elif 'x' in string_to_compute: 
    l = string_to_compute.split('x')[0] 
    r = string_to_compute.split('x')[1] 
    entry.insert(0, float(l) * float(r)) 
elif '/' in string_to_compute: 
    l = string_to_compute.split('/')[0] 
    r = string_to_compute.split('/')[1] 
    entry.insert(0, float(l)/float(r)) 


def set_text(text): 
e.insert(tk.END, text) 
return 

e = tk.Entry(win, width=35) 
e.grid(columnspan=4) 

seven_button = tk.Button(win, width=6, text="7", command=lambda: set_text("7")).grid(row=1, column=0) 
eight_button = tk.Button(win, width=6, text="8", command=lambda: set_text("8")).grid(row=1, column=1) 
nine_button = tk.Button(win, width=6, text="9", command=lambda: set_text("9")).grid(row=1, column=2) 
div_button = tk.Button(win, width=6, text="/", command=lambda: set_text("/")).grid(row=1, column=3) 

four_button = tk.Button(win, width=6, text="4", command=lambda: set_text("4")).grid(row=2, column=0) 
five_button = tk.Button(win, width=6, text="5", command=lambda: set_text("5")).grid(row=2, column=1) 
six_button = tk.Button(win, width=6, text="6", command=lambda: set_text("6")).grid(row=2, column=2) 
multiply_button = tk.Button(win, width=6, text="x", command=lambda: set_text("x")).grid(row=2, column=3) 

one_button = tk.Button(win, width=6, text="1", command=lambda: set_text("1")).grid(row=3, column=0) 
two_button = tk.Button(win, width=6, text="2", command=lambda: set_text("2")).grid(row=3, column=1) 
three_button = tk.Button(win, width=6, text="3", command=lambda: set_text("3")).grid(row=3, column=2) 
minus_button = tk.Button(win, width=6, text="-", command=lambda: set_text("-")).grid(row=3, column=3) 

zero_button = tk.Button(win, width=14, text="0", command=lambda: set_text("0")).grid(columnspan=2) 
point_button = tk.Button(win, width=6, text=".", command=lambda: set_text(".")).grid(row=4, column=2) 
plus_button = tk.Button(win, width=6, text="+", command=lambda: set_text("+")).grid(row=4, column=3) 
equal_button = tk.Button(win, width=6, text="=", command=lambda: compute(e)).grid(row=5, column=1) 


win.mainloop() 

請注意我改變了分隔符號和一些條目的邏輯,所以它會進入從左到右。

+0

woops,你沒有問如何計算,只是如何輸入正確的......哦。 – jwillis0720

0

只需在最後插入文字即可。

def set_text(text): 
    e.insert(END,text) 
    return