屏幕键盘上有2个条目的python tkinter

时间:2018-04-04 11:35:30

标签: python tkinter

我遇到了问题,我想在屏幕键盘上使用2个条目。

我有这个代码,这正是我所寻找的,但仅限于一个条目。可以请有人编辑代码,以便我可以使用相同的键盘两个条目? 我确实尝试自己做,但当我点击第二个条目并弹出键盘然后我尝试写东西时,我写的所有内容都会转到第一个条目。

非常感谢

import tkinter as tk
from functools import partial

root = tk.Tk()
root.geometry("200x100")

num_run = 0
btn_funcid = 0


def click(btn):
    global num_run
    text = "%s" % btn
    if not text == "Del" and not text == "Close":
        e.insert(tk.END, text)
    if text == 'Del':
        e.delete(0, tk.END)
    if text == 'Close':
        boot.destroy()
        num_run = 0
        root.unbind('<Button-1>', btn_funcid)


def numpad():
    global num_run, boot
    boot = tk.Tk()
    boot['bg'] = 'green'
    lf = tk.LabelFrame(boot, text=" keypad ", bd=3)
    lf.pack(padx=15, pady=10)
    btn_list = [
        '7',  '8',  '9',
        '4',  '5',  '6',
        '1',  '2',  '3',
        '0',  'Del',  'Close']
    r = 1
    c = 0
    n = 0
    btn = list(range(len(btn_list)))
    for label in btn_list:
        cmd = partial(click, label)
        btn[n] = tk.Button(lf, text=label, width=10, height=5, command=cmd)
        btn[n].grid(row=r, column=c)
        n += 1
        c += 1
        if c == 3:
            c = 0
            r += 1


def close(event):
    global num_run, btn_funcid
    if num_run == 1:
        boot.destroy()
        num_run = 0
        root.unbind('<Button-1>', btn_funcid)


def run(event):
    global num_run, btn_funcid
    if num_run == 0:
        num_run = 1
        numpad()
        btn_funcid = root.bind('<Button-1>', close)

rand=tk.StringVar()
rand1=tk.StringVar()



e=tk.Entry(root, font='Verdana  8 bold',textvariable=rand, insertwidth=4, justify='right')
e.bind('<Button-1>', run)
e.place(x=10, y=10)
secondentry= tk.Entry(root, font='Verdana  8 bold',textvariable=rand1, insertwidth=4, justify='right')
secondentry.place(x=10, y=30)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您始终致电e.insert / e.delete

您必须选择要写入的条目并写入该条目。

e.g。

  • selected_entry作为全局变量
  • 绑定点击事件/扩展click_event,以便同时进行
  • 在回调集selected_entry内部到事件回调源(调用者)
  • 使用selected_entry.insert / selected_entry.delete

根据您的代码,以下4个步骤就足够了:

1.定义selected_entry

#[...]

# inside your global section (head)
# define the "selected_entry"

insert_entry = None

#[...]

2.适应点击事件

# [...]

def click(btn):
    global num_run
    global insert_entry
    if insert_entry is None:
        return
    text = "%s" % btn
    if not text == "Del" and not text == "Close":
        insert_entry.insert(tk.END, text)
    if text == 'Del':
        insert_entry.delete(0, tk.END)
    if text == 'Close':
        boot.destroy()
        num_run = 0
        root.unbind('<Button-1>', btn_funcid)

# [...]

3.调整run函数以设置insert_entry

# [...]

def run(event):
    global num_run, btn_funcid
    global insert_entry
    insert_entry = event.widget # use the caller to adapt it
    if num_run == 0:
        num_run = 1
        numpad()
        btn_funcid = root.bind('<Button-1>', close)

# [...]

4.修复“main”

中的绑定
# [...]

secondentry.bind('<Button-1>', run) # just like for the first one

# [...]