Python 4输入框数字键盘

Python 4输入框数字键盘,python,tkinter,tkinter-entry,numeric-keypad,Python,Tkinter,Tkinter Entry,Numeric Keypad,我正在尝试为我的步进电机控制器制作一个GUI。我完全没有编程方面的背景,但在这里阅读并观看了几篇教程后,我生成了一段代码,它的外观和行为都符合我的想法 代码的一部分在下面。挑战是什么?至少对我来说:当我选择“时间推移”按钮时,窗口会打开,并显示一些尚未起作用的按钮。还有4个输入框和一个键盘。在这里我遇到了麻烦:当我选择一个输入框时,我想用键盘输入数字。我想自己稍后整理DEL按钮。我想我需要绑定/定义/插入/命令,但我不知道如何使用。此时,键盘仅填充输入框4 import Tkinter as t

我正在尝试为我的步进电机控制器制作一个GUI。我完全没有编程方面的背景,但在这里阅读并观看了几篇教程后,我生成了一段代码,它的外观和行为都符合我的想法

代码的一部分在下面。挑战是什么?至少对我来说:当我选择“时间推移”按钮时,窗口会打开,并显示一些尚未起作用的按钮。还有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()
我使用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()
插入索引对应于条目中光标的当前位置

编辑:下面是使用我的解决方案的Page3类的完整代码。我把set_text作为一个方法,我把它放在了uu init_uuuuuuuuuuuuuuuuuu3的e1,…,e4属性中,这样在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方法是不是一个好主意,因为当我运行代码时,没有重叠的小部件是很复杂的。条目被标签覆盖了一半。我建议您切换到自动并排放置小部件而不重叠的包或网格。

您好,欢迎使用stackoverflow!试着把你的问题表述为一个问题,并试着省去那些不能澄清你确切问题的文本。我试图将你的代码实现到我的代码中,但我无法让它工作。你能用我的代码作为起点来帮助我吗?谢谢你的帮助,它很有效!根据你的回答,我得到了Del函数。