4个输入框数字小键盘

时间:2017-02-04 18:53:44

标签: python tkinter tkinter-entry numeric-keypad

我正在尝试为我的步进电机控制器制作GUI。我完全没有编程的背景,但是在这里阅读并看了几个教程,我制作了一段代码,它看起来和我认为它应该做的行为。

部分代码在这里。挑战是什么? (至少对我而言):当我选择“Time Lapse”按钮时,窗口会打开并显示几个没有功能的按钮。还有4个入口盒和一个键盘。在这里,我正在撞墙:当我选择一个输入框时,我想用键盘输入数字。 (我想稍后自己整理DEL按钮)。我想我需要绑定/定义/插入/命令,但我不知道如何。此时,键盘仅填充输入框4。

import Tkinter as tk
from Tkinter import *

LARGE_FONT = ("Verdana", 12, 'underline', 'bold')
SMALL_FONT = ('Helvetica', 10, 'underline')

# MTC = Macro Time Controller

class MTC(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Macro Slider - Time Lapse - Controller")

        container = tk.Frame(self, borderwidth=5, relief="sunken")
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, Page3):
            frame = F(container, self,)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="news")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Main Menu", font=LARGE_FONT)
        label.pack(pady=30, padx=30)

        button = tk.Button(self, text="Steppermotor Test", font=SMALL_FONT,
                           command=lambda: controller.show_frame(Page1))
        button.place(x=50, y=125, height=80, width=110)

        button2 = tk.Button(self, text="Macro Stacking", font=SMALL_FONT,
                            command=lambda: controller.show_frame(Page2))
        button2.place(x=250, y=125, height=80, width=110)

        button3 = tk.Button(self, text="Time Lapse", font=SMALL_FONT,
                            command=lambda: controller.show_frame(Page3))
        button3.place(x=450, y=125, height=80, width=110)

        button4 = tk.Button(self, text="In Development", font=SMALL_FONT,
                            command=lambda: controller.show_frame(Page4))
        button4.place(x=650, y=125, height=80, width=110)

        button5 = tk.Button(self, text="Quit", font=SMALL_FONT,
                            command=self.quit)
        button5.place(x=350, y=300, height=80, width=110)


class Page3(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Time Lapse!!!", font=LARGE_FONT)
        label.pack(pady=30, padx=30)

        def on_entry_click1(event):
            """function that gets called whenever entry is clicked"""
            if e1.get() == 'Enter settings 1...':
                e1.delete(0, "end")  # delete all the text in the entry
                e1.insert(0, '')  # Insert blank for user input
                e1.config(fg='black')

        def on_entry_click2(event):
            """function that gets called whenever entry is clicked"""
            if e2.get() == 'Enter settings 2...':
                e2.delete(0, "end")  # delete all the text in the entry
                e2.insert(0, '')  # Insert blank for user input
                e2.config(fg='black')

        def on_entry_click3(event):
            """function that gets called whenever entry is clicked"""
            if e3.get() == 'Enter settings 3...':
                e3.delete(0, "end")  # delete all the text in the entry
                e3.insert(0, '')  # Insert blank for user input
                e3.config(fg='black')

        def on_entry_click4(event):
            """function that gets called whenever entry is clicked"""
            if e4.get() == 'Enter settings 4...':
                e4.delete(0, "end")  # delete all the text in the entry
                e4.insert(0, '')  # Insert blank for user input
                e4.config(fg='black')

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

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

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

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

        def toggle():
            if t_btn.config('text')[-1] == 'Forward':
                t_btn.config(text='Reverse')
            else:
                t_btn.config(text='Forward')

        button1 = Button(self, text="Back to Main Menu",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack(padx=50, pady=15)

        tk.Radiobutton(self, text="Full Step", indicatoron=0, width=6, padx=20, value=1, variable=1).place(x=20, y=150)

        tk.Radiobutton(self, text="Half Step", indicatoron=0, width=6, padx=20, value=2, variable=1).place(x=110, y=150)

        tk.Radiobutton(self, text="Quarter Step", indicatoron=0, width=6, padx=20, value=3, variable=1).place(x=200, y=150)

        tk.Radiobutton(self, text="Either Step", indicatoron=0, width=6, padx=20, value=4, variable=1).place(x=290, y=150)

        tk.Radiobutton(self, text="Sixteenth Step", indicatoron=0, width=6, padx=20, value=5, variable=1).place(x=380, y=150)

        t_btn = Button(self, text="Forward", width=12, command=toggle)
        t_btn.place(x=250, y=350)

        tk.button = Button(self, text='Move', width=12).place(x=350, y=350)

        e1 = Entry(self, bd=1, width=10, justify=RIGHT)
        e1.insert(0, 'Enter settings 1...')
        e1.bind('<FocusIn>', on_entry_click1)
        e1.config(fg='red')
        e1.place(x=25, y=200)

        label1 = tk.Label(self, text="Input 1")
        label1.place(x=100, y=200)

        e2 = Entry(self, bd=1, width=10, justify=RIGHT)
        e2.insert(0, 'Enter settings 2...')
        e2.bind('<FocusIn>', on_entry_click2)
        e2.config(fg='red')
        e2.place(x=25, y=225)

        label2 = tk.Label(self, text="Input 2")
        label2.place(x=100, y=225)

        e3 = Entry(self, bd=1, width=10, justify=RIGHT)
        e3.insert(0, 'Enter settings 3...')
        e3.bind('<FocusIn>', on_entry_click3)
        e3.config(fg='red')
        e3.place(x=25, y=250)

        label3 = tk.Label(self, text="Input 3")
        label3.place(x=100, y=250)

        e4 = Entry(self, bd=1, width=10, justify=RIGHT)
        e4.insert(0, 'Enter settings 4...')
        e4.bind('<FocusIn>', on_entry_click4)
        e4.config(fg='red')
        e4.place(x=25, y=275)

        label4 = tk.Label(self, text="Input 4")
        label4.place(x=100, y=275)

# Numeric Keypad

        sev = tk.Button(self, bd=3, text="7", command=lambda:set_text("7"))
        sev.place(x=600, y=150, width=50, height=50)
        eig = tk.Button(self, bd=3, text="8", command=lambda:set_text("8"))
        eig.place(x=650, y=150, width=50, height=50)
        nin = tk.Button(self, bd=3, text="9", command=lambda:set_text("9"))
        nin.place(x=700, y=150, width=50, height=50)

        fou = tk.Button(self, bd=3, text="4", command=lambda:set_text("4"))
        fou.place(x=600, y=200, width=50, height=50)
        fiv = tk.Button(self, bd=3, text="5", command=lambda:set_text("5"))
        fiv.place(x=650, y=200, width=50, height=50)
        six = tk.Button(self, bd=3, text="6", command=lambda:set_text("6"))
        six.place(x=700, y=200, width=50, height=50)

        one = tk.Button(self, bd=3, text="1", command=lambda:set_text("1"))
        one.place(x=600, y=250, width=50, height=50)
        two = tk.Button(self, bd=3, text="2", command=lambda:set_text("2"))
        two.place(x=650, y=250, width=50, height=50)
        thr = tk.Button(self, bd=3, text="3", command=lambda:set_text("3"))
        thr.place(x=700, y=250, width=50, height=50)

        dot = tk.Button(self, bd=3, text=".", command=lambda:set_text("."))
        dot.place(x=600, y=300, width=50, height=50)
        zer = tk.Button(self, bd=3, text="0", command=lambda:set_text("0"))
        zer.place(x=650, y=300, width=50, height=50)
        bck = tk.Button(self, bd=3, text="Del")
        bck.place(x=700, y=300, width=50, height=50)

app = MTC()
app.geometry("800x480+200+0")
app.mainloop()

1 个答案:

答案 0 :(得分:1)

我使用focus_get函数来获取具有键盘焦点的小部件,因为它是所选条目。然后我在这个条目中插入数字。以下是一个例子:

import Tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.entries = [tk.Entry(self) for i in range(4)]
        for i,e in enumerate(self.entries):
            e.grid(row=i, column=0)

        keypad_frame = tk.Frame(self)
        keypad_frame.grid(row=0, rowspan=4, column=1, sticky="eswn")

        tk.Button(keypad_frame, text="7", command=lambda: self.set_text("7")).grid(row=0, column=0)
        tk.Button(keypad_frame, text="8", command=lambda: self.set_text("8")).grid(row=0, column=1)
        tk.Button(keypad_frame, text="9", command=lambda: self.set_text("9")).grid(row=0, column=2)
        tk.Button(keypad_frame, text="4", command=lambda: self.set_text("4")).grid(row=1, column=0)
        tk.Button(keypad_frame, text="5", command=lambda: self.set_text("5")).grid(row=1, column=1)
        tk.Button(keypad_frame, text="6", command=lambda: self.set_text("6")).grid(row=1, column=2)
        tk.Button(keypad_frame, text="1", command=lambda: self.set_text("1")).grid(row=2, column=0)
        tk.Button(keypad_frame, text="2", command=lambda: self.set_text("2")).grid(row=2, column=1)
        tk.Button(keypad_frame, text="3", command=lambda: self.set_text("3")).grid(row=2, column=2)
        tk.Button(keypad_frame, text="0", command=lambda: self.set_text("0")).grid(row=3, column=1)

        self.mainloop()

    def set_text(self, text):
        widget = self.focus_get()
        if widget in self.entries:
            widget.insert("insert", text)

if __name__  == "__main__":      
    App()

"insert"索引对应于条目中光标的当前位置。

编辑:以下是使用我的解决方案的Page3课程的完整代码。我将set_text作为一种方法(我把它放在__init__之外),我创建了e1的{​​{1}},...,e4属性,以便{ {1}},我们可以检查具有焦点的窗口小部件是否是其中之一。

Page3

备注:我不确定使用set_text方法进行布局是个好主意,因为没有重叠的小部件很复杂(当我运行代码时,条目是一半被标签覆盖)。我建议您切换到class Page3(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text="Time Lapse!!!", font=LARGE_FONT) label.pack(pady=30, padx=30) def toggle(): if t_btn.config('text')[-1] == 'Forward': t_btn.config(text='Reverse') else: t_btn.config(text='Forward') button1 = Button(self, text="Back to Main Menu", command=lambda: controller.show_frame(StartPage)) button1.pack(padx=50, pady=15) tk.Radiobutton(self, text="Full Step", indicatoron=0, width=6, padx=20, value=1, variable=1).place(x=20, y=150) tk.Radiobutton(self, text="Half Step", indicatoron=0, width=6, padx=20, value=2, variable=1).place(x=110, y=150) tk.Radiobutton(self, text="Quarter Step", indicatoron=0, width=6, padx=20, value=3, variable=1).place(x=200, y=150) tk.Radiobutton(self, text="Either Step", indicatoron=0, width=6, padx=20, value=4, variable=1).place(x=290, y=150) tk.Radiobutton(self, text="Sixteenth Step", indicatoron=0, width=6, padx=20, value=5, variable=1).place(x=380, y=150) t_btn = Button(self, text="Forward", width=12, command=toggle) t_btn.place(x=250, y=350) tk.button = Button(self, text='Move', width=12).place(x=350, y=350) self.e1 = Entry(self, bd=1, width=10, justify=RIGHT) self.e1.insert(0, 'Enter settings 1...') self.e1.config(fg='red') self.e1.place(x=25, y=200) label1 = tk.Label(self, text="Input 1") label1.place(x=100, y=200) self.e2 = Entry(self, bd=1, width=10, justify=RIGHT) self.e2.insert(0, 'Enter settings 2...') self.e2.config(fg='red') self.e2.place(x=25, y=225) label2 = tk.Label(self, text="Input 2") label2.place(x=100, y=225) self.e3 = Entry(self, bd=1, width=10, justify=RIGHT) self.e3.insert(0, 'Enter settings 3...') self.e3.config(fg='red') self.e3.place(x=25, y=250) label3 = tk.Label(self, text="Input 3") label3.place(x=100, y=250) self.e4 = Entry(self, bd=1, width=10, justify=RIGHT) self.e4.insert(0, 'Enter settings 4...') self.e4.config(fg='red') self.e4.place(x=25, y=275) label4 = tk.Label(self, text="Input 4") label4.place(x=100, y=275) # Numeric Keypad sev = tk.Button(self, bd=3, text="7", command=lambda:self.set_text("7")) sev.place(x=600, y=150, width=50, height=50) eig = tk.Button(self, bd=3, text="8", command=lambda:self.set_text("8")) eig.place(x=650, y=150, width=50, height=50) nin = tk.Button(self, bd=3, text="9", command=lambda:self.set_text("9")) nin.place(x=700, y=150, width=50, height=50) fou = tk.Button(self, bd=3, text="4", command=lambda:self.set_text("4")) fou.place(x=600, y=200, width=50, height=50) fiv = tk.Button(self, bd=3, text="5", command=lambda:self.set_text("5")) fiv.place(x=650, y=200, width=50, height=50) six = tk.Button(self, bd=3, text="6", command=lambda:self.set_text("6")) six.place(x=700, y=200, width=50, height=50) one = tk.Button(self, bd=3, text="1", command=lambda:self.set_text("1")) one.place(x=600, y=250, width=50, height=50) two = tk.Button(self, bd=3, text="2", command=lambda:self.set_text("2")) two.place(x=650, y=250, width=50, height=50) thr = tk.Button(self, bd=3, text="3", command=lambda:self.set_text("3")) thr.place(x=700, y=250, width=50, height=50) dot = tk.Button(self, bd=3, text=".", command=lambda:self.set_text(".")) dot.place(x=600, y=300, width=50, height=50) zer = tk.Button(self, bd=3, text="0", command=lambda:self.set_text("0")) zer.place(x=650, y=300, width=50, height=50) bck = tk.Button(self, bd=3, text="Del") bck.place(x=700, y=300, width=50, height=50) def set_text(self, text): widget = self.focus_get() if widget in [self.e1, self.e2, self.e3, self.e4]: widget.insert("insert", text) place,并自动将小部件并排放置而不会重叠。