tkinter python 中的数字键盘 [英] numpad in tkinter python

查看:53
本文介绍了tkinter python 中的数字键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个小键盘链接到我的 Python 代码中的输入字段,但我无法让它工作.我希望有人能帮我解决这个问题.提前致谢!

I am trying to link a numpad to the entry field in my Python code, but i cant get it working. I hope someone can help me with this. Thanks in advance!

我的代码

import tkinter as tk
from PIL import ImageTk
from PIL.ImageTk import PhotoImage
from tkinter import Button


# background

FILENAME = 'C:\Volledig Project\Sealapparaat-800x480.jpg'
root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=480)
canvas.pack()
tk_img = ImageTk.PhotoImage(file=FILENAME)
canvas.create_image(400, 240, image=tk_img)


entry1 = tk.Entry(root)
entr1_windows = canvas.create_window(30, 300, anchor="nw", window=entry1)

muted3 = False


def mute_picture():
    global muted3
    if muted3:
        volumeKnop3.configure(image=volumePhoto3)
        muted3 = False
        root.after(int(entry1.get()), mute_picture)
    else:
        volumeKnop3.configure(image=mutePhoto3)
        muted3 = True

root.after(0, mute_picture)


mutePhoto3 = PhotoImage(file="C:\Volledig Project\Time_Done.png")
volumePhoto3 = PhotoImage(file="C:\Volledig Project\Time_Not_Done.png")

volumeKnop3 = Button(image=volumePhoto3, command=mute_picture, background="white", activebackground="White", border=0)
volumeKnop3_window = canvas.create_window(30, 200, anchor="nw", window=volumeKnop3)

root.mainloop()

我已经找到并尝试了几个小键盘,但我不能让一个工作,所以我希望有人有一个好的小键盘,并且知道我如何在我的代码中实现它

推荐答案

这是我几年前为 Raspberry Pi 触摸屏应用程序编写的小键盘示例.它有效(不过我现在可以改进代码).

This was a numpad example I wrote a few years ago for a Raspberry Pi touch screen application. It works (I could improve upon the code now though).

单击输入字段以显示数字键盘.要使其在您的代码中工作,请将 tk.Entryentry1 实例替换为我的 NumpadEntry.应该就是这么简单.例如

Click on an entry field to bring up the Numpad. To make it work in your code, replace your entry1 instance of tk.Entry with my NumpadEntry. Should be as simple as that. For example

entry1 = NumpadEntry(root)

为添加 enumerate_row_column 生成器而进行了细微更改.旧的方法不是很pythonic.

Slight change made to add the enumerate_row_column generator. Old way of doing it wasn't very pythonic.

from tkinter import *
from tkinter import simpledialog

def enumerate_row_column(iterable, num_cols):
    for idx, item in enumerate(iterable):
        row = idx // num_cols
        col = idx % num_cols
        yield row,col,item

class NumpadEntry(Entry):
    def __init__(self,parent=None,**kw):
        Entry.__init__(self,parent,**kw)
        self.bind('<FocusIn>',self.numpadEntry)
        self.bind('<FocusOut>',self.numpadExit)
        self.edited = False
    def numpadEntry(self,event):
        if self.edited == False:
            print("You Clicked on me")
            self['bg']= '#ffffcc'
            self.edited = True
            new = numPad(self)
        else:
            self.edited = False
    def numpadExit(self,event):
        self['bg']= '#ffffff'

class numPad(simpledialog.Dialog):
    def __init__(self,master=None,textVariable=None):
        self.top = Toplevel(master=master)
        self.top.protocol("WM_DELETE_WINDOW",self.ok)
        self.createWidgets()
        self.master = master
        
    def createWidgets(self):
        btn_list = ['7',  '8',  '9', '4',  '5',  '6', '1',  '2',  '3', '0',  'Close',  'Del']
        # create and position all buttons with a for-loop
        btn = []
        # Use custom generator to give us row/column positions
        for r,c,label in enumerate_row_column(btn_list,3):
            # partial takes care of function and argument
            cmd = lambda x = label: self.click(x)
            # create the button
            cur = Button(self.top, text=label, width=10, height=5, command=cmd)
            # position the button
            cur.grid(row=r, column=c)                                              
            btn.append(cur)
            
    def click(self,label):
        print(label)
        if label == 'Del':
            currentText = self.master.get()
            self.master.delete(0, END)
            self.master.insert(0, currentText[:-1])
        elif label == 'Close':
            self.ok()
        else:
            currentText = self.master.get()
            self.master.delete(0, END)
            self.master.insert(0, currentText+label)
    def ok(self):
        self.top.destroy()
        self.top.master.focus()

class App(Frame):
    def __init__(self,parent=None,**kw):
        Frame.__init__(self,parent,**kw)
        self.textEntryVar1 = StringVar()
        self.e1 = NumpadEntry(self,textvariable=self.textEntryVar1)
        self.e1.grid()

        self.textEntryVar2 = StringVar()
        self.e2 = NumpadEntry(self,textvariable=self.textEntryVar2)
        self.e2.grid()


if __name__ == '__main__':
    root = Tk()
    root.geometry("200x100")
    app = App(root)
    app.grid()
    root.mainloop()

这篇关于tkinter python 中的数字键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆