1

I got this sort of edgy scenario I would like to accomplish in my tinkter app. The way my tinkter app would work is: a user needs to enter in some text. Then select a radio button option. Once a user selects the radio button option some value would be calculated. Now here is the edge case. Basically if a user were to select a radio option without entering a value I would like to show an error message "you need to enter a value into the input box" below the input box.

a snapshot of what I mean: enter image description here

Here is what my code looks like:

from tkinter import *

class GetInterfaceValues():
    def __init__(self):
        self.root = Tk()
        self.totalValue = StringVar()


        self.root.geometry('900x500')
        self.RadioText = StringVar()

        self.getPeg = Button(self.root, text='calculate kegs values', command=self.findPeg)


        self.quarterlyTextString = 'Keg'
        self.yearlyTextString = 'parKeg'

        self.textInputBox = Text(self.root, relief=RIDGE, height=1, width=6, borderwidth=2)
        self.frequencyText = Label(self.root, text="Frequency")
        self.normalKegRadioButton = Radiobutton(self.root, text="normal Keg", variable=self.RadioText,
                                                value=self.quarterlyTextString, command=self.selectedRadioButtonOption)
        self.parRadioButton = Radiobutton(self.root, text="Par Kegs", variable=self.RadioText, value=self.yearlyTextString,
                                             command=self.selectedRadioButtonOption)
        self.clearButton = Button(self.root, text="Clear",command=self.clear)

        self.textInputBox.pack()
        self.normalKegRadioButton.pack()
        self.parRadioButton.pack()

        self.getPeg.pack()
        self.clearButton.pack()
        self.root.mainloop()

    def selectedRadioButtonOption(self):
        radioButtonFrequencyOption = self.RadioText.get()

        if(radioButtonFrequencyOption == self.quarterlyTextString):
            self.findPeg()
        if(radioButtonFrequencyOption == self.yearlyTextString):
            print(self.yearlyTextString)


    def getTextInput(self):
        result = self.textInputBox.get("1.0", "end")
        results = result.upper()
        results = results.rstrip()
        results = int(results)

        return results


    def clear(self):
        self.parRadioButton.deselect()
        self.normalKegRadioButton.deselect()
        self.textInputBox.delete("1.0", "end")

    def findPeg(self):
        userInput = self.getTextInput()

        lab = userInput * 15
        print(lab)

app = GetInterfaceValues()
app.mainloop()
1
  • You already have a callback, def selectedRadioButtonOption. What is stopping you to add a else condition to display your message? Commented May 1, 2020 at 13:16

1 Answer 1

1

You can test if a entry field contains text by using:

if len(entry.get()) > 0:

You can also invoke a function whenever a radiobutton (or entry) is changed using:

myradiobutton = Radiobutton(root, command = thefunction)

Additionally, you can programmatically deselect a radiobutton with:

myradiobutton.deselect()

if that helps.

Putting this together you could do something like this:

def getinput():
    if len(entry.get()) > 0:
        #do whatever you want to do with the input
    else:
        radiobutton.deselect()
        #display your "enter something" message

entry = Entry(root)
radiobutton = Radiobutton(root, text = "Click when done typing", command = getinput)
entry.pack()
radiobutton.pack()

Let me know if I'm missing the point here. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Hey mate, thank you for the response. This is exactly what i am looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.