2

I'm studying about 'tkinter' recently. Pycharm told me here is no 'messagebox', but 'messagebox.py' does exist in my 'tkinter'. Many ways I gained from Internet can't solve this. Please help me, I'll appreciate for that.

greet_button = Button(
    table,
    text = 'Yes',
    width = 20,
    height = 2,
    bg = 'yellow',
    fg = 'red',
    command = table.messagebox.showinfo(title='Hello', message='Hello!')
)
LiritoLotus
  • 23
  • 1
  • 3

1 Answers1

3

Some packages of tkinter need a extra import like ttk, ttkthemes or messagebox.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
def err():
    tk.messagebox.showinfo(title='hello', message='hello')

erro = tk.Button(root, text="Top", command=err)
erro.pack()

root.mainloop()

As oneliner you would need an annonymus function:

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()

erro = tk.Button(root, text="Top", command=lambda:tk.messagebox.showinfo(title='hello', message='hello'))
erro.pack()

root.mainloop()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • 1
    ummm...isnt `themedtk` part of `ttkthemes` ? and not `tkinter` directly? – Delrius Euphoria Sep 04 '20 at 14:12
  • I had the `python AttributeError: 'App' object has no attribute` error not about a messagebox, but something else. I cannot trace it back anymore, but I know that importing `ttk` or anything else did not help. I think it was rather a problem of the order and indentation, or somehow linked to [Either Enter or button trigger "TypeError ... missing 1 required positional argument" or "TypeError ... takes 0 positional arguments but 1 was given"](https://stackoverflow.com/a/76276569/11154841). – questionto42 May 17 '23 at 23:59
  • 1
    @questionto42 what attribute is missing? Based on the context you provided I guess it is missing attribute "tk" and you have done something wrong by subclassing a widget. [You can join the tkinter chat on stackoverflow](https://chat.stackoverflow.com/rooms/249491/tkinter) – Thingamabobs May 18 '23 at 09:01