2013-12-11 3 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

당신은 계산 방법을 묻지 않았습니다. 올바르게 입력하는 방법은 ... 오. – jwillis0720

0

끝에 텍스트를 삽입하기 만하면됩니다.

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