0

I am trying to do a simple program. I am trying to check if radio button 1 is selected show a button and if radio button 2 selected and if there is a button on the screen disappear it. Please help me.

from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
bati = tkinter.Tk()
bati.geometry('500x500')
bati.title('Project')
def hello():
    messagebox.showinfo("Say Hello", "Hello World")

def askfile():
    bati.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    lb2 = Label(bati, text=bati.filename, fg='red', font=("Times", 10, "bold"))
    lb2.place(x='270',y='34')


def first():
    b = Button(bati, text='Import', activebackground='red', bd='3', bg='gray', fg='yellow', font=("Times New Roman", 10, "bold"), command=askfile)
    b.place(x='200',y='30')
    working = True

def second():
    if working == True:
        b.widget.pack_forget()
        working = False

canvas_width = 3000
canvas_height = 220
w = Canvas(bati, width=canvas_width,height=canvas_height)
w.pack()


y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y, fill="#476042", width='2')

v = IntVar()
v.set('L')

rb1 = Radiobutton(bati, text='Import A File', value=1, variable=v, command=first, font=("Comic Sans MS", 10, "bold"))
rb2 = Radiobutton(bati, text='Choose From Web', value=2, variable=v, command=second, font=("Comic Sans MS", 10, "bold"))
rb1.place(x='50',y='30')
rb2.place(x='50',y='50')
working = False


bati.mainloop()
  • `b` and `working` are currently local variables within `first()`, and vanish when that function exits. `second()` cannot possibly access them. – jasonharper Oct 09 '19 at 17:03
  • so what do I need to do? – TheScientist Ashe Oct 09 '19 at 17:10
  • Hint: why does `bati.filename` work inside `askfile`? What's different between how you handle `bati`, and how you handle `b`? Specifically: *where in the code* does `bati` get its initial value? Is that inside a function, or outside? – Karl Knechtel Oct 09 '19 at 17:20

1 Answers1

0

Issues:

  • Local variables will not work for this - you need to remember the button state outside of first and second, so that you can use it the next time.

  • We show the button with .place, so we should similarly hide it with .place_forget instead of .pack_forget.

  • The .place position should be given with integers instead of strings. Similarly for the button's bd, i.e. border width (in pixels, i.e. a number of pixels).

  • Event handlers are supposed to receive an event parameter, even if you ignore it. The .widget you've written in your second command is presumably copied from some other code that tries to find out the widget to hide from the event data (e.g. here). But that .widget would be the one that sent the command, i.e. the Radiobutton, not the Button that you want to hide.

What we will do is create the button ahead of time, in a global variable (in a more serious project, you should consider using classes instead, and then you would use a class member to remember it). Since the button should start out not visible, we just don't .place it right away, but instead use .place in first and .place_forget in second. So:

b = Button(
    bati, text='Import', activebackground='red', bd=3,
    bg='gray', fg='yellow', font=("Times New Roman", 10, "bold"),
    command=askfile
)

def first(event):
    b.place(x=200,y=30)

def second():
    b.place_forget()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153