How to fix the next_verse button doesn't get disabled?

43 Views Asked by At

The program is supposed to search words or verses in Quran and return results when the "Search" button is clicked. The problem is when you search and get results and after that you search a word or a verse that doesn't exist and you get no results. So then the next_verse button which shows the next verse that contains this word should be disabled but the point that it doesn't get disabled.

The tkinter button named next_verse doesn't get disabled although I changed its state to 'disabled' but it was still active and I printed the button state to see if it was changed and it printed 'disabled' although it was active. line[49]

The problem is I want this button to be disabled because it produces an error and ruins the programe when it is clicked because simply there is no verse that should be displayed.

I tried to update the frame and the main window and I expected that to solve the problem but it didn't.

Here's the code:

import json
import tkinter as tk
from tkinter import ttk
from urllib.request import urlopen
from pyarabic.araby import strip_diacritics
from pyarabic.araby import strip_small
from pyarabic.araby import strip_tatweel
from pyarabic.araby import normalize_alef
import time

index = 0
firstTime = True
nextBtns = []

Search_window = tk.Tk()
Search_window.title("Search Quran")

MainFrame = ttk.Frame(Search_window)
MainFrame.grid(column=0, row=0, sticky="nsew")
frames = [MainFrame]

def Previous():
    global index
    index -= 1
    frames[index].tkraise()

def Next():
    global index
    index += 1
    frames[index].tkraise()

search_label = tk.Label(MainFrame, text="Enter a word or an aya to search")
search_label.grid(column=1, row=2)
verseEntry = ttk.Entry(MainFrame)
verseEntry.grid(column=1, row=3)


def Search():
    global label1
    global label2
    global frames
    global index
    global firstTime
    global next_verse
    global nextBtns
    isMainFrame = True
    repitition = False
    if not firstTime:
        next_verse['state'] = 'disabled'
        print(next_verse['state'])
        label1.destroy()
        label2.destroy()
        nextBtns = [next_verse]
        time.sleep(3)
        MainFrame.update()
        Search_window.update()
        if len(frames)>1:
            for frame in frames:
                if not isMainFrame:
                    frame.grid_forget()
                    frame.destroy()
                else:
                    isMainFrame = False
            frames = [MainFrame]        
    word = ' ' + verseEntry.get()
    for surah in range(len(data)):
        for verse in data[surah]["verses"]:
            verse_text = strip_diacritics(verse["text"])
            verse_text = normalize_alef(verse_text)
            verse_text = strip_small(verse_text)
            verse_text = strip_tatweel(verse_text)
            result = word + ' ' in verse_text or word + 'ا' in verse_text
            if result:
                print(verse["text"])
                print("سورة " + data[surah]["name"], f"آية رقم {verse['id']}")
                if firstTime:
                    next_verse = ttk.Button(MainFrame, text="<<",state='disabled',  command=Next)
                    next_verse.grid(column=0, row=2, sticky=tk.W)
                    nextBtns.append(next_verse)
                    firstTime = False
                else:
                    next_btn = ttk.Button(frames[index], text="<<", state='disabled', command=Next)
                    next_btn.grid(column=0, row=2, sticky=tk.E)
                    nextBtns.append(next_btn)
                    nextBtns[-2]['state'] = 'normal'
                if repitition:
                    frame = ttk.LabelFrame(Search_window)
                    frame.grid(column=0, row=0, sticky=tk.NSEW)
                    ttk.Label(frame, text=verse["text"], wraplength=300, width = 50, anchor='center',                               justify="right").grid(column=1, row=0)
                    ttk.Label(frame, text="سورة " + data[surah]["name"] + ' ' + f"آية رقم {verse['id']}", anchor='center', justify="right").grid(column=1, row=1)
                    ttk.Button(frame, text=">>", command=Previous).grid(column=2, row=2, sticky=tk.E)
                    frames.append(frame)
                    index += 1
                else:
                    label1 = ttk.Label(MainFrame, text=verse["text"], wraplength=300, anchor='center', justify="right")
                    label1.grid(column=1, row=0)
                    label2 = ttk.Label(MainFrame, text="سورة " + data[surah]["name"] + ' ' + f"آية رقم {verse['id']}", anchor='center', justify="right")
                    label2.grid(column=1, row=1, sticky=tk.NSEW)
                    repitition = True
    MainFrame.tkraise()
    index = 0
beginSearch = ttk.Button(MainFrame, text="Search", command=Search)
beginSearch.grid(column=2, row=2)

jsonFile = urlopen("https://cdn.jsdelivr.net/npm/[email protected]/dist/quran.json")
response = jsonFile.read()
data = json.loads(response)

Search_window.mainloop()
0

There are 0 best solutions below