Python Forum
[Tkinter] Radio button help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Radio button help
#1
Hi everyone, Python noob here, im working on an assignment that is Tkinter heavy. Basically the endstate of the program is to display a GUI that allows a user to select upcoming entertainment events from data from an imported live HTML page that displays dates and a names of events using findall to display the dates and name.(like a music concert website for example.

The program also must feature an offline mode that when selected tells the program to gather the "data" from an offline source, in my case a downloaded HTML page from a sitdown comedy club.

So to get to the point of my problem. Im having trouble figuring out how to tell the radio buttons to read from the offline source when offline is selected and read from the online source when the online button is selected.
Ive tried multiple things, using if statements and different commands and functions. But im at the point where I pull my hair out when I stare at it for too long.

In this scenario the online source is there to simulate a live HTML page but they're both downloaded pages.

Below is the code I believe is pertinent to my problem.

The error I get when I run the program now is:
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Muzz\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\Muzz\Documents\Python\Assigment 2\Tkinter\ProtoAssignment2\test1.py", line 65, in comedy_pressed Comedian1 = findall('">([A-Za-z]+ [A-Za-z]+ [A-Za-z]+) @',Source) File "C:\Users\Muzz\AppData\Local\Programs\Python\Python37-32\lib\re.py", line 223, in findall return _compile(pattern, flags).findall(string) TypeError: expected string or bytes-like object
Thanks in advance to anyone who takes the time to read this and help me.

from tkinter import *
from re import *
#Make the menu
entertainment_fix=Tk()


#Give menu a name----------------------------------------------------------------------------------#
entertainment_fix.title('Entertainment Fix!')
#Give the menu a background colour
entertainment_fix['bg']='light sea green'

#Insert a picture
fixpic=PhotoImage(file='entertainment.gif')
picture=Label(entertainment_fix, image=fixpic)
picture.grid(row=0, column =1)    
   

Source = ()
# Create a Interger Value for the radio buttons
On_or_Off = IntVar()
# Define a function that when called by the offline radio button, the source HTML page changes------------#
def offline():
    if On_or_Off.get() == 1:
        
        Source = open('OfflineComedy.html').read()
        
# Define a function that when called by the online radio button, the source HTML page changes-------------#    
def online():
    if On_or_Off.get() == 2:
        Source = open('OnlineComedy.html').read()
        
                    
# Create a radio button for working offline----------------------------------------------#

work_offline_button = Radiobutton(entertainment_fix,text='Work Offline',font=('Arial',10),
                                  variable = On_or_Off, value = 1,
                                  command = offline)
work_offline_button.grid(row=0,column=0)
# Create a radio button for working offline----------------------------------------------#

work_online_button = Radiobutton(entertainment_fix,text='Work Online',font=('Arial',10),
                                  variable = On_or_Off, value = 2,
                                  command = online)
work_online_button.grid(row=0,column=2)
Reply
#2
The error is showing Comedian1 = findall('">([A-Za-z]+ [A-Za-z]+ [A-Za-z]+) @',Source) as being on line 65 and that it expected string or bytes-like object being passed to it, this line is not in the code shown.
Reply
#3
Is that the complete error traceback? It doesn't seem to reference any line in your source code.
It almost looks like it's for another module as error seems to be related to the regular expression.
Reply
#4
To those who replied to my thread, I should of posted the entire program. Its not too big. Thanks for taking a look.

from tkinter import *
from re import *
#Make the menu
entertainment_fix=Tk()


#Give menu a name----------------------------------------------------------------------------------#
entertainment_fix.title('Entertainment Fix!')
#Give the menu a background colour
entertainment_fix['bg']='light sea green'

#Insert a picture
fixpic=PhotoImage(file='entertainment.gif')
picture=Label(entertainment_fix, image=fixpic)
picture.grid(row=0, column =1)    
   

Source = ()
# Create a Interger Value for the radio buttons
On_or_Off = IntVar()
# Define a function that when called by the offline radio button, the source HTML page changes------------#
def online_offline():
    if On_or_Off.get() == 1:
        Source = open('OfflineComedy.html').read()
    elif On_or_Off.get() == 2:
        Source = open('OnlineComedy.html').read()
        
                    
# Create a radio button for working offline----------------------------------------------#

work_offline_button = Radiobutton(entertainment_fix,text='Work Offline',font=('Arial',10),
                                  variable = On_or_Off, value = 1,
                                  command = online_offline)
work_offline_button.grid(row=0,column=0)
# Create a radio button for working offline----------------------------------------------#

work_online_button = Radiobutton(entertainment_fix,text='Work Online',font=('Arial',10),
                                  variable = On_or_Off, value = 2,
                                  command = online_offline)
work_online_button.grid(row=0,column=2)







#Define a function that opens a window when the Comedy button is pressed
def comedy_pressed():  
    comedy_guide= Toplevel()
    comedy_guide.geometry('750x500')
    comedy_guide['bg']='green'
    global comedy_choose
    comedy_guide.title('The Paddo Tavern')
#Create a title label for Comedy
    comedy_title = Label(comedy_guide,text='The Paddo Tavern',font=('Arial',20))
    comedy_title.grid(row=0,column=0,sticky=N)
          
#Create a checkbox for the first show
    
    Comedian1 = findall('">([A-Za-z]+ [A-Za-z]+ [A-Za-z]+) @',Source)
    date1 = findall('">([A-Za-z]+ [1-30-9a-z]+ [A-Za-z&; A-Za-z2-30-11-21-9 1-12\.1-60]+)',Source)
    first_show=Checkbutton(comedy_guide,text=('WHO:',Comedian1[2],'WHEN:',date1[3]),
                            font=('Arial',12))
    first_show.grid(row=1,column=0, pady=15,sticky=W)
#Create a checkbox for the second show
    Comedian2=findall('">([A-Za-z]+ [A-Za-z]+)',Source)
    date2 = findall('">([A-Za-z]+ [1-30-9a-z]+ [A-Za-z&; A-Za-z2-30-11-21-9 1-12\.1-60]+)',Source)
    second_show=Checkbutton(comedy_guide,text=('WHO:',Comedian2[15],'WHEN:',date2[4]),
                            font=('Arial',12))
    second_show.grid(row=2,column=0, pady=15,sticky = W)
    
#Create a checkbox for the third show
    Comedian3=findall('">([A-Za-z]+ [A-Za-z]+)',Source)
    date3 = findall('">([A-Za-z]+ [1-30-9a-z]+ [A-Za-z&; A-Za-z2-30-11-21-9 1-12\.1-60]+)',Source)
    third_show=Checkbutton(comedy_guide,text=('WHO:',Comedian3[16],'WHEN:',date3[5]),
                            font=('Arial',12))
    third_show.grid(row=3,column=0, pady=15,sticky = W)

#Create a checkbox for the fourth show
    Comedian4=findall('">([A-Za-z]+ [A-Za-z]+ [A-Za-z]+ )',Source)
    date4 = findall('">([A-Za-z]+ [1-30-9a-z,]+ [A-Za-z&; A-Za-z2-30-11-21-9 1-12\.1-60]+)',Source)
    fourth_show=Checkbutton(comedy_guide,text=('WHO:',Comedian4[6],'WHEN:',date4[7]),
                            font=('Arial',12))
    fourth_show.grid(row=4,column=0,pady=15, sticky = W)
    
#Create a checkbox for the fourth show
    Comedian5=findall('">([A-Za-z]+ [A-Za-z]+ )',Source)
    date5 = findall('">([A-Za-z]+ [1-30-9a-z,]+ [A-Za-z&; A-Za-z2-30-11-21-9 1-12\.1-60]+)',Source)
    fourth_show=Checkbutton(comedy_guide,text=('WHO:',Comedian5[10],'WHEN:',date5[8]),
                            font=('Arial',12))
    fourth_show.grid(row=5,column=0,pady=15, sticky = W)
#---------------------------------------------------------------------------------------------------------#          

        
    

    
  
    




#Create a Movie button
movie_choose=Button(entertainment_fix,text='Choose A Movie!',
                    font=('Arial',12))
                    
movie_choose.grid(row = 1, column = 0)
#Create a Music button
music_choose=Button(entertainment_fix,text='Choose A Music Event!',
                    font=('Arial',12))

music_choose.grid(row = 1, column = 1)
#Create a Comedy button
comedy_choose=Button(entertainment_fix,text='Choose A Comedy Show!',
                    font=('Arial',12),
                    command=comedy_pressed)
comedy_choose.grid(row = 1, column = 2)



entertainment_fix.mainloop()
Reply
#5
The variable named Source is local to the function, i.e. is garbage collected when the function exits because you don't return it, hence is None and not a string or bytes like object. I would strongly suggest that you learn classes before attempting Tkinter as you can declare variables as an instance object which can be used through out the class, instead of being garbage collected. Finally, read the Python Style Guide when you have time https://www.python.org/dev/peps/pep-0008/ Variables are lower_case_with_underlines. You code implies that Source, because of the upper case, is a class, not a variable.
Reply
#6
I understood the problem almost immediately on reading your reply, Ill look into it further, thanks for getting back to me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Is there a way to determine if a radio button has been selected? TWB 5 5,022 Jan-31-2023, 09:44 AM
Last Post: Vadanane
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 2,315 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Grid the radio buttons Joni_Engr 6 4,778 Nov-24-2021, 07:20 PM
Last Post: menator01
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,521 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  problem with radio button crook79 3 3,711 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  [Tkinter] I can't get information from a radio button aquerci 2 2,755 May-20-2020, 10:31 AM
Last Post: aquerci
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,012 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 11,829 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020