Reputation: 155
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.
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.
Upvotes: 1
Views: 4257
Reputation: 15098
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.
Upvotes: 1