1
votes

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?

2
did you bother to google any of this? Everything you asked for is on the first page of google results with each question placed into google search...Mike - SMT
@SierraMountainTech I'm sorry but google is a search engine that just provides with the info that I'm looking for. It doesn't Explain the code. I'm a learner. Not a professional. So for everything I learn, I need an explanation so that I can use my knowledge to make another project later on.S.Ahmed
You learn by reading documentation and then creating the code to see what it does. I am not a professional either. I have been doing this in my free time for about 4 months now and its all been "read, write, run, fix"Mike - SMT
Assuming that you are running on windows, if you change the file extension from .py to .pyw the command window will no longer appear.scotty3785
@scotty3785 appreciated.S.Ahmed

2 Answers

3
votes

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)

1
votes

Don't use from tkinter import * it's really not recommended because it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can (and it will) cause name collisions.

Have the right reflexes, use import tkinter or import tkinter as tk instead

this should work, you have to use the disabledbackground option :

import tkinter as tk

root = tk.Tk()

display = tk.Entry(root,font=('Arial', 20, 'bold'), disabledbackground='lightblue', state='disabled')
display.pack()

root.resizable(0,0)
root.mainloop()