1

I am trying to make the user input a string for my implemented DFA diagram, and I want the input box to be displayed along with its output right under it in the same GUI box. I'm only including one of my functions to show how I used tkinter's messagebox to display the output.

import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox
import tkinter

def q3(s, i) :  
    if (i == len(s)) :  
        tkinter.messagebox.showinfo('Answer',"REJECTED")
        return;  

    if (s[i] == 'a') : 
        q3(s, i + 1);  
    elif (s[i] == 'b'):
        q3(s, i + 1);   

root = tk.Tk()
root.withdraw()
user_inp = simpledialog.askstring(title="DFA", prompt="Enter a string within the alphabet {a,b}* Suffix must be bb")

This code works to display the results correctly, like this. enter image description here enter image description here

AKA, when the user inputs the string and presses OK, it opens another GUI to display the results. Please help me so that I can have a GUI that displays both in the same box. It's even harder because all of my DFA functions (I have one for q0, q1, q2, and q3) have different results for the output as rejected/accepted. I'm not sure if I have to create a variable for the each of them. (The only one that is accepted is q2). I'm also not sure if I need to use any Labels. In your answer, if there's imports I must include, please include that as well.

9
  • In which window do you want to show the results on? The messagebox? Commented Oct 4, 2020 at 15:17
  • @CoolCloud I want to show the results just under the OK button for the simpledialog box. Or really, as long as they are displayed in any box together. Just want them displayed together in the same pop-up. So under OK, it would say "Result: REJECTED" (or ACCEPTED depending on the result for each of the states q0,q1,q2,q3 in the DFA diagram. This adds trickiness for me.) Commented Oct 4, 2020 at 15:24
  • Unfortunately they cannot be edited. Commented Oct 4, 2020 at 15:26
  • @CoolCloud There is no way to have input and output in the same box? Commented Oct 4, 2020 at 15:28
  • 1
    @CoolCloud Hi, it still seems like no one else added any input. Would you be able to please help me further on this? Commented Oct 4, 2020 at 21:28

1 Answer 1

1

Well I tried to understand what you really want, but couldnt get it, so I put together what I feel that you might want, with an example:

from tkinter import *

class Custombox:
    def __init__(self, title, text):
        self.title = title
        self.text = text

        def store():
            self.new = self.entry.get() #storing data from entry box onto variable
            if self.new == 'Hello World': #checking 
                a.change('ACCEPTED') #changing text
            else: 
                a.change('REJECTED') #else, changing text
            
        self.win = Toplevel()
        self.win.title(self.title)
        # self.win.geometry('400x150')
        self.win.wm_attributes('-topmost', True)

        self.label = Label(self.win, text=self.text)
        self.label.grid(row=0, column=0, pady=(20, 10),columnspan=3,sticky='w',padx=10)

        self.l = Label(self.win)

        self.entry = Entry(self.win, width=50)
        self.entry.grid(row=1, column=1,columnspan=2,padx=10)

        self.b1 = Button(self.win, text='Ok', width=10,command=store)
        self.b1.grid(row=3, column=1,pady=10)

        self.b2 = Button(self.win, text='Cancel', width=10,command=self.win.destroy)
        self.b2.grid(row=3, column=2,pady=10)
        
    def __str__(self): 
        return str(self.new)

    def change(self,ran_text):
        self.l.config(text=ran_text,font=(0,12))
        self.l.grid(row=2,column=1,columnspan=3,sticky='nsew',pady=5)


root = Tk()
root.withdraw()

a = Custombox('Custom box', 'Enter a string within the alphabet {a,b}*. Suffix must be bb.')

root.mainloop()

Over here im creating a window using basic tkinter properties and placements and all you have to understand is that store() inside that class is similar to your q3() as I couldnt understand what was going on there, I just made my own function. So you will have to replace store() with what works for you, but do not change the self.new = self.entry.get(). Yes this might seem a bit shady, but it was the quickest i could do, because im a beginner as well.

Anyways here, a has the value of whatever you type into the entry widget, BUT while using a, make sure to use str(a) or you wont get correct results as type(a) returns <class '__main__.Custombox'>. Do let me know if you face any difficulty in the implementation of this class. I know there are mistakes here, feel free to edit those mistakes out or let me know.

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

1 Comment

Very helpful thank you! Just had to input my own function and it works well. Thanks so much!

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.