gpt4 book ai didi

python - 如何禁用键盘输入到 Entry 小部件,禁用调整 tkinter 窗口的大小并隐藏控制台窗口?

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:36 26 4
gpt4 key购买 nike

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

  • 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?

到目前为止,这是我的代码...

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()

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

我想让我的计算器有点色彩。我更改了 root 窗口的颜色、按钮以及 Entry 小部件的颜色。即使在禁用后,是否有任何方法可以更改小部件的颜色?

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

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

最佳答案

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

将状态设置为禁用:

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.

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

关于python - 如何禁用键盘输入到 Entry 小部件,禁用调整 tkinter 窗口的大小并隐藏控制台窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com