Eric HB
Eric HB

Reputation: 887

tkinter button to run command and destroy popup window

I am working in Python 3 with tkinter and would like to have a button in a popup window both run a command and destroy the popup window. The code I have below works on the frontend, but gives an error on the command line, is this an issue that is resolveable?

The error that I get is:

Traceback (most recent call last):
File "C:Python34\lib\tkinter__intit__.py", line 1538 in __call__
return self.func(*args)
File "test.py", line 14, in
command = lambda: display_something() * popup.destroy())
TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'

from tkinter import *

class MainView(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        new_popup = Button(self,text = 'Make A Popup!',
            command = lambda: popup()).grid(row=0,column=0)


def popup():
    popup = Toplevel()
    button = Button(popup, text = 'Display something on the command line',
        command = lambda: display_something() & popup.destroy())
    button.pack()

def display_something():
    print('popup ran the command')

def main():
    root = Tk()
    root.title('Eric\'s Archiver')
    app = MainView(root)
    root.mainloop()


if __name__ == '__main__':
    main()

Upvotes: 3

Views: 3499

Answers (1)

furas
furas

Reputation: 142631

& is "and" operator but for bits, not for booleans.

You need boolean operator and.

But in your code or should work better because first function returns None which is treated as False - and False and anything gives always False so there is no need to execute anything. But False or anything may gives False or True depending on anything so it has to execute anything to get final result.).

lambda: display_something() or popup.destroy()

Upvotes: 3

Related Questions