0

I am developing a program in which specific elements are to be extracted from live HTML pages, shown to the user in the GUI and then on the selection of the user, they are to be printed in an HTML document. I am able to perform most of these, need help with a small problem. In the code presented below, I have extracted one title from a website and have used Checkbox function to display this in the GUI, if the checkbox is ticked by the user, it should be printed in the HTML document. However, the value of the checkbox always remains "False" whether it is checked or unchecked and I am unable to proceed further with the code. Can you please highlight where the issue is? I implemented the save_function just to check the value of the Checkbox and it is always "False".

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.gui()

    def game_function(self):
        game = Tk()
        game.geometry('600x400')
        game.title('Upcoming Video Game Releases')
        game.resizable(0,0)

        if self.check.get() == 0:

            self.check_g_1 = BooleanVar()

            url = "https://www.metacritic.com/browse/games/release-date/coming-soon/all/date"
            user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
            request = urllib.request.Request(url,headers={'User-Agent': user_agent})
            response = urllib.request.urlopen(request)
            encoding = response.info().get_param('charset', 'utf8')
            html = response.read().decode(encoding)

            date_title_1 = re.findall('<span>(.*?)</span>',html,re.DOTALL)[1]
            title_1 = re.findall('<h3>(.*?)</h3>',html,re.DOTALL)[0]
            title1_subtitle = Checkbutton(game, text = title_1.lstrip(), fg='black', font = ('Arial', 12), justify=LEFT, variable=self.check_g_1)
            title1_subtitle.place(relx=0, rely=0)
            date_1 = Label(game, text = date_title_1.lstrip(), fg='black', font = ('Arial', 12))
            date_1.place(relx=0.6, rely=0)

       else:
            print("FFF")

       save_button = Button(game, text = 'Save', font = ('Arial', 14), fg='black', bg='white', relief = 'solid', command=self.save_function)
       save_button.place(relx=0.38, rely=0.72, height=60, width=200)
       save_button.configure(wraplength='200')

    def save_function(self):
        print(self.check_g_1.get())

I am concerned with the value self.check_g_1

6
  • What is self.check and self.check_g_1? There's isn't enough code in your question to answer it. Please create a minimal reproducible example that reproduces the issue. Commented May 11, 2019 at 8:46
  • self_check is another checkbox variable but I have no issue with that. I am concerned with self.check_g_1. I have presented enough code in the question. Please see the variable "title1_subtitle = Checkbutton..." and you will see self.check_g_1 assigned. Commented May 11, 2019 at 8:48
  • 1
    Sorry I missed the usage of self.check_g_1 because you're not following the PEP 8 - Style Guide for Python Code recommendations which makes it difficult to read the code (especially here on this site). Regardless, it's hardly "minimal"—all that urllib and re stuff likely has nothing to do with the issue, and so only obscures the possibly important stuff. Commented May 11, 2019 at 9:00
  • Rest of the code is just the initialisation of the GUI and buttons/labels. There is currently nothing else. Do you want to see that as well or can you help me with this issue? Commented May 11, 2019 at 9:07
  • 1
    No, I don't want to see that, just am example with the minimum amount of code in it that will reproduce the problem, not help you debug your project. Commented May 11, 2019 at 9:13

1 Answer 1

1

Your problem is that you are creating the variable in one tkinter instance and trying to use it in another. Instances of Tk can't share widgets or variables.

As a rule of thumb you should never create two instances of Tk. You need to replace your second call to Tk with a call to Toplevel.

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

1 Comment

I did realised it after posting the question. Thanks anyways.

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.