如何在使用输入框时禁用键盘尝试

huangapple go评论70阅读模式
英文:

How can i disable to keyboard attempts while using entrybox

问题

以下是您要的代码的翻译部分:

import tkinter as tk
from tkinter import *

root = tk.Tk()
text = ' '
root.state('zoomed')
root.resizable(False, False)
root.configure(bg='gray95')
entry_page = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray')
canvas1 = Canvas(width=700, height=650, bg='dark gray')
canvas1.place(x=415, y=60)

def button_1():
    entry_page.insert(len(str(entry_page)), 1)

def button_2():
    entry_page.insert(len(str(entry_page)), 2)

def button_3():
    entry_page.insert(len(str(entry_page)), 3)

def button_4():
    entry_page.insert(len(str(entry_page)), 4)

def button_5():
    entry_page.insert(len(str(entry_page)), 5)

def button_6():
    entry_page.insert(len(str(entry_page)), 6)

def button_7():
    entry_page.insert(len(str(entry_page)), 7)

def button_8():
    entry_page.insert(len(str(entry_page)), 8)

def button_9():
    entry_page.insert(len(str(entry_page)), 9)

def button_0():
    entry_page.insert(len(str(entry_page)), 0)

def button_clear():
    entry_page.delete(0, END)

def button_delete():
    entry_page.delete(len(entry_page.get()) - 1, END)

def button_ok():
    pass

button1 = tk.Button(root, text='1', width=10, height=5, command=button_1)
button2 = tk.Button(root, text='2', width=10, height=5, command=button_2)
button3 = tk.Button(root, text='3', width=10, height=5, command=button_3)
button4 = tk.Button(root, text='4', width=10, height=5, command=button_4)
button5 = tk.Button(root, text='5', width=10, height=5, command=button_5)
button6 = tk.Button(root, text='6', width=10, height=5, command=button_6)
button7 = tk.Button(root, text='7', width=10, height=5, command=button_7)
button8 = tk.Button(root, text='8', width=10, height=5, command=button_8)
button9 = tk.Button(root, text='9', width=10, height=5, command=button_9)
button0 = tk.Button(root, text='0', width=10, height=5, command=button_0)
button_clear = tk.Button(root, text='AC', width=10, height=5, command=button_clear)
button_delete = tk.Button(root, text='<-', width=10, height=5, command=button_delete)
button_ok = tk.Button(root, text='✓', width=39, height=2, command=button_ok)

def toggle_keyboard():
    canvas1.place_forget()

    entry_page.place(x=1200, y=100)
    button_clear.place(x=1200, y=160)
    button1.place(x=1300, y=160)
    button2.place(x=1400, y=160)
    button3.place(x=1200, y=260)
    button4.place(x=1300, y=260)
    button5.place(x=1400, y=260)
    button6.place(x=1200, y=360)
    button7.place(x=1300, y=360)
    button8.place(x=1400, y=360)
    button9.place(x=1200, y=460)
    button0.place(x=1300, y=460)
    button_delete.place(x=1400, y=460)
    button_ok.place(x=1200, y=560)

toggle_keyboard()
root.mainloop()

希望这对您有所帮助。如果您有其他问题,请随时提出。

英文:

import tkinter as tk
from tkinter import *

root = tk.Tk()
yazı = &#39; &#39;
root.state(&#39;zoomed&#39;)
root.resizable(False, False)
root.configure(bg=&#39;gray95&#39;)
labelsayfa = Entry(root, text=&#39;&#39;, width=28, font=&#39;Calibri 15&#39;, bg=&#39;dark gray&#39;)
c1 = Canvas(width=700, height=650, bg=&#39;dark gray&#39;)
c1.place(x=415, y=60)
def b1g():
labelsayfa.insert(len(str(labelsayfa)), 1)
def b2g():
labelsayfa.insert(len(str(labelsayfa)), 2)
def b3g():
labelsayfa.insert(len(str(labelsayfa)), 3)
def b4g():
labelsayfa.insert(len(str(labelsayfa)), 4)
def b5g():
labelsayfa.insert(len(str(labelsayfa)), 5)
def b6g():
labelsayfa.insert(len(str(labelsayfa)), 6)
def b7g():
labelsayfa.insert(len(str(labelsayfa)), 7)
def b8g():
labelsayfa.insert(len(str(labelsayfa)), 8)
def b9g():
labelsayfa.insert(len(str(labelsayfa)), 9)
def b0g():
labelsayfa.insert(len(str(labelsayfa)), 10)
def bacg():
labelsayfa.delete(0, END)
def bsilg():
labelsayfa.delete(len(labelsayfa.get()) - 1, END)
def bokg():
pass
b1 = tk.Button(root, text=&#39;1&#39;, width=10, height=5, command=b1g)
b2 = tk.Button(root, text=&#39;2&#39;, width=10, height=5, command=b2g)
b3 = tk.Button(root, text=&#39;3&#39;, width=10, height=5, command=b3g)
b4 = tk.Button(root, text=&#39;4&#39;, width=10, height=5, command=b4g)
b5 = tk.Button(root, text=&#39;5&#39;, width=10, height=5, command=b5g)
b6 = tk.Button(root, text=&#39;6&#39;, width=10, height=5, command=b6g)
b7 = tk.Button(root, text=&#39;7&#39;, width=10, height=5, command=b7g)
b8 = tk.Button(root, text=&#39;8&#39;, width=10, height=5, command=b8g)
b9 = tk.Button(root, text=&#39;9&#39;, width=10, height=5, command=b9g)
b0 = tk.Button(root, text=&#39;0&#39;, width=10, height=5, command=b0g)
bac = tk.Button(root, text=&#39;AC&#39;, width=10, height=5, command=bacg)
bsil = tk.Button(root, text=&#39;&lt;--&#39;, width=10, height=5, command=bsilg)
bok = tk.Button(root, text=&#39;✓&#39;, width=39, height=2, command=bokg)
def tag():
c1.place_forget()
labelsayfa.place(x=1200, y=100)
bac.place(x=1200, y=160)
b1.place(x=1300, y=160)
b2.place(x=1400, y=160)
b3.place(x=1200, y=260)
b4.place(x=1300, y=260)
b5.place(x=1400, y=260)
b6.place(x=1200, y=360)
b7.place(x=1300, y=360)
b8.place(x=1400, y=360)
b9.place(x=1200, y=460)
b0.place(x=1300, y=460)
bsil.place(x=1400, y=460)
bok.place(x=1200, y=560)
tag()
root.mainloop()

I want to use textbox without keyboard only with buttons but i dont know how can i to do.How can i prevent the keyboard please help me i have searched all of the solitions but i couldnt find.Sorry about dots......................................................................................................

答案1

得分: 1

你可以通过将输入小部件的state参数设置为DISABLED来关闭输入小键盘。

labelsayfa = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray', state=DISABLED)

然而,这将导致你的按钮也无法工作,所以你可以在按钮函数内部,临时启用输入小部件的状态,执行按钮操作,然后在退出函数之前将状态恢复为DISABLED

def b1g():
    labelsayfa.configure(state=NORMAL)
    labelsayfa.insert(len(str(labelsayfa)), 1)
    labelsayfa.configure(state=DISABLED)

或者,为了避免在每个函数中重复相同的两行代码,你可以创建一个函数包装器,但这只会将重复的行数从2减少到1,所以这更多地取决于个人喜好。

例如:

def toggle_state(func):
    def wrapper():
        labelsayfa.configure(state=NORMAL)
        func()
        labelsayfa.configure(state=DISABLED)
    return wrapper

@toggle_state
def b1g():
    labelsayfa.insert(len(str(labelsayfa)), 1)

@toggle_state
def b2g():
    labelsayfa.insert(len(str(labelsayfa)), 2)

# 其他按钮函数也可以使用@toggle_state装饰器

这段代码创建了一个函数包装器toggle_state,用于暂时启用和禁用输入小部件的状态,以便执行按钮操作。然后,每个按钮函数使用@toggle_state装饰器来包装,以实现相同的效果。

英文:

You can turn off keyboard input on an entry widget by setting it's state parameter to DISABLED.

labelsayfa = Entry(root, text=&#39;&#39;, width=28, font=&#39;Calibri 15&#39;, bg=&#39;dark gray&#39;, state=DISABLED)

This will make your buttons not work as well though, so what you can do is inside your button functions, temporarily enable the entry widgets state, perform the buttons actions and then return the state back to DISABLED before exiting the function.

def b1g():
labelsayfa.configure(state=NORMAL)
labelsayfa.insert(len(str(labelsayfa)), 1)
labelsayfa.configure(state=DISABLED)

Or to avoid repeating the same two lines in every function you can create a function wrapper also, but this would only reduce the repeated lines from 2 to 1, so it really is more of a personal preference.

For example:

root = tk.Tk()
yazı = &#39; &#39;
root.state(&#39;zoomed&#39;)
root.resizable(False, False)
root.configure(bg=&#39;gray95&#39;)
labelsayfa = Entry(root, text=&#39;&#39;, width=28, font=&#39;Calibri 15&#39;, bg=&#39;dark gray&#39;, state=DISABLED)
c1 = Canvas(width=700, height=650, bg=&#39;dark gray&#39;)
c1.place(x=415, y=60)
def toggle_state(func):
def wrapper():
labelsayfa.configure(state=NORMAL)
func()
labelsayfa.configure(state=DISABLED)
return wrapper
@toggle_state
def b1g():
labelsayfa.insert(len(str(labelsayfa)), 1)
@toggle_state
def b2g():
labelsayfa.insert(len(str(labelsayfa)), 2)
@toggle_state
def b3g():
labelsayfa.insert(len(str(labelsayfa)), 3)
@toggle_state
def b4g():
labelsayfa.insert(len(str(labelsayfa)), 4)
@toggle_state
def b5g():
labelsayfa.insert(len(str(labelsayfa)), 5)
@toggle_state
def b6g():
labelsayfa.insert(len(str(labelsayfa)), 6)
@toggle_state
def b7g():
labelsayfa.insert(len(str(labelsayfa)), 7)
@toggle_state
def b8g():
labelsayfa.insert(len(str(labelsayfa)), 8)
@toggle_state
def b9g():
labelsayfa.insert(len(str(labelsayfa)), 9)
@toggle_state
def b0g():
labelsayfa.insert(len(str(labelsayfa)), 10)
@toggle_state
def bacg():
labelsayfa.delete(0, END)
@toggle_state
def bsilg():
labelsayfa.delete(len(labelsayfa.get()) - 1, END)
@toggle_state
def bokg():
pass
b1 = tk.Button(root, text=&#39;1&#39;, width=10, height=5, command=b1g)
b2 = tk.Button(root, text=&#39;2&#39;, width=10, height=5, command=b2g)
b3 = tk.Button(root, text=&#39;3&#39;, width=10, height=5, command=b3g)
b4 = tk.Button(root, text=&#39;4&#39;, width=10, height=5, command=b4g)
b5 = tk.Button(root, text=&#39;5&#39;, width=10, height=5, command=b5g)
b6 = tk.Button(root, text=&#39;6&#39;, width=10, height=5, command=b6g)
b7 = tk.Button(root, text=&#39;7&#39;, width=10, height=5, command=b7g)
b8 = tk.Button(root, text=&#39;8&#39;, width=10, height=5, command=b8g)
b9 = tk.Button(root, text=&#39;9&#39;, width=10, height=5, command=b9g)
b0 = tk.Button(root, text=&#39;0&#39;, width=10, height=5, command=b0g)
bac = tk.Button(root, text=&#39;AC&#39;, width=10, height=5, command=bacg)
bsil = tk.Button(root, text=&#39;&lt;--&#39;, width=10, height=5, command=bsilg)
bok = tk.Button(root, text=&#39;✓&#39;, width=39, height=2, command=bokg)
def tag():
c1.place_forget()
labelsayfa.place(x=1200, y=100)
bac.place(x=1200, y=160)
b1.place(x=1300, y=160)
b2.place(x=1400, y=160)
b3.place(x=1200, y=260)
b4.place(x=1300, y=260)
b5.place(x=1400, y=260)
b6.place(x=1200, y=360)
b7.place(x=1300, y=360)
b8.place(x=1400, y=360)
b9.place(x=1200, y=460)
b0.place(x=1300, y=460)
bsil.place(x=1400, y=460)
bok.place(x=1200, y=560)
tag()
root.mainloop()

答案2

得分: 0

只添加 " state='readonly' ":

Entry(root, text='', state='readonly')
英文:

As far as I understood the question, just add " state='readonly' "

Entry(root, text=&#39;&#39;, state=&#39;readonly&#39;)

huangapple
  • 本文由 发表于 2023年2月24日 05:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定