如何禁用输入到 Entry 小部件的键盘输入,禁用 tkinter 窗口的大小调整并隐藏控制台窗口? [英] How do I disable keyboard input into an Entry widget, disable resizing of tkinter window and hide the console window?

查看:75
本文介绍了如何禁用输入到 Entry 小部件的键盘输入,禁用 tkinter 窗口的大小调整并隐藏控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tkinter 制作一个计算器,我希望执行以下操作:

<块引用>
  • 禁用 Entry 小部件的键盘输入,以便用户只能通过按钮输入.
  • 即使在为 Entry 小部件禁用键盘输入后,我仍希望能够更改小部件的背景和前景.
  • 我想隐藏控制台窗口,因为它在使用这个计算器时完全没有用.
  • 我不想让用户调整 root 窗口的大小.如何禁止调整 root 窗口的大小?

这是我目前的代码...

from tkinter import *根 = Tk()root.title("计算器")root.config(背景=黑色")运算符 = ""textVar = StringVar()def valInput(number):全球运营商运算符+=str(数字)textVar.set(运算符)display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")display.grid(row=0, column=0, columnspan=4)btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))btn7.grid(row=1, column=0)"""还有更多按钮..."""root.mainloop()

如您所见,我可以使用按钮输入 Entry 小部件,但稍后,在计算器完成后,如果用户输入像 abcd... 这样的字符,则会导致问题并显示错误.如何禁止键盘输入以避免这些错误?

我想让我的计算器变得丰富多彩.我更改了 root 窗口的颜色、按钮以及 Entry 小部件的颜色.即使在禁用后,有没有办法更改小部件的颜色?

我在使用这个计算器时不需要控制台窗口.我如何隐藏它?

如果我调整root 窗口的大小,计算器会变得丑陋,此外,不需要调整窗口大小.那么如何防止用户调整窗口大小?

解决方案

能够在 Entry(args) 中禁用键盘输入

设置状态为禁用:

display = Entry(root, state=DISABLED)

<块引用>

能够禁用调整 tkinter 窗口大小的功能(以便您无法拖动和拉伸它.

root.resizable(0,0)

<块引用>

能够使命令提示符窗口消失.(我只想要 tkinter 窗口.

使用 .pyw 扩展名重命名文件(假设您使用的是 Windows)

I am making a calculator using tkinter and I wish to do the following:

  • Disable keyboard input for the Entry widget so that the user can only input through the buttons.
  • Even after disabling keyboard input for the Entry widget, I wish to be able to change the background and foreground of the widget.
  • I wish to hide the console window because it is totally useless in the use of this calculator.
  • I don't want to let the user resize the root window. How do I disallow the resizing of the root window?

Here is my code so far...

from tkinter import *

root = Tk()
root.title("Calculator")
root.config(background="black")
operator = ""
textVar = StringVar()
def valInput(number):
    global operator
    operator+=str(number)
    textVar.set(operator)

display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")
display.grid(row=0, column=0, columnspan=4)
btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))
btn7.grid(row=1, column=0)

"""
And more buttons...

"""
root.mainloop()

As you can see, I can input into the Entry widget using buttons but later on, after the calculator is complete, if the user inputs characters like abcd... it will cause problems and show errors. How do I disallow keyboard entry so that I can avoid these errors?

I want to make my calculator a bit colorful. I changed the color of the root window, the buttons and also the color of the Entry widget. Is there any way to change the color of the widget even after it is disabled?

I don't need the console window while using this calculator. How do I hide it?

If I resize the root window, the calculator becomes ugly, besides, resizing the window isn't necessary. So how do I prevent the user from resizing the window?

解决方案

To be able to disable keyboard input in Entry(args)

Set the state to disabled:

display = Entry(root, state=DISABLED)

To be able to disable the feature of resizing the tkinter window (so that you can't drag and stretch it.

root.resizable(0,0)

To be able to make the command prompt window disappear. (I just want the tkinter window.

Rename the file with a .pyw extension (assuming you are using windows)

这篇关于如何禁用输入到 Entry 小部件的键盘输入,禁用 tkinter 窗口的大小调整并隐藏控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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