-2

I want to make a toolbar and a button at the same time in the same window but It won't work. I tried everything but I cannot find anything. Can I do that in tkinter. If I can't can you guys give some other things to make that happen.

`from tkinter import *
from tkinter import messagebox
from tkinter import Tk, Frame, Menu

def call_me():
    answer = messagebox.askyesnocancel("exit", "Do you really want to exit")
    if(answer):
            root.quit()



class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.master.title("Simple menu")

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)
        fileMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenu)


    def onExit(self):

        self.quit()



def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example()
    root.mainloop()


if __name__ == '__main__':
    main()


root = Tk()
b = Button(root, text="message", command=call_me)
b.pack()
root.geometry("400x400+120+120")
root.mainloop()

`

when i run this code it opens a window with a button. when i click on that button it should exit but it opens a whole new window with a toolbor in it.

Emir Sürmen
  • 884
  • 3
  • 14
  • 33
  • Have you had a look at http://zetcode.com/tkinter/menustoolbars/ Right down the bottom - is that roughly what you are looking for? – cup Mar 31 '20 at 17:28
  • yes, i did look at that. it works as a toolbal but i can't let the code work with the button code that i made at the same time – Emir Sürmen Mar 31 '20 at 17:47
  • There's nothing in tkinter to prevent you from making a "toolbar and a button" at the same time in the same window. Can you explain what you tried, and how the result is different than what you expect? – Bryan Oakley Mar 31 '20 at 17:59
  • [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged), [how-to-create-child-window-and-communicate-with-parent-in-tkinter](https://stackoverflow.com/questions/10718073) – stovfl Mar 31 '20 at 18:54

1 Answers1

0

Use this:

def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example()
    b = Button(root, text="message", command=call_me)
    b.pack()
    root.mainloop()

And delete everything after main(). Learn how tkinter works. Take a look at pygame. Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24