-8

I have been trying to make a graphics window for my python code but I don't know how to add buttons and I have looked at websites and it hasn't been for my edition (3.2.3) I have got some code now, is there anything wrong with this?

import tkinter
import tkinter as tk
root = tk.Tk()
root.title("Adventure game")
root.geometry("1820x1000")
root.mainloop()
class Window(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.geometry("600x400+30+30")
        wButton = Button(self, text='start', command = self.OnButtonClick())
        wButton.pack()

        def OnButtonClick(self):
            top = Toplevel()
            top.title("title")
            top.geometry("300x150+30+30")
            topButton = Button(top, text="OPTION_1", command = self.OnButtonPress())
            topButton = Button(top, text="OPTION_2", command = self.OnButtonPress())
            topButton.pack()

            def OnButtonPress(self):
                top = Toplevel()
                top.title("title")
                top.geometry("300x150+30+30")
                topButton = Button(top, text="OPTION_1", command = self.destroy)
                bottomButton = Button(top, text="OPTION_2", command = self.destroy)
                topButton.pack()
Luke
  • 1
  • 1
  • *"is there anything wrong with this?"* - why are you asking us?! Have you *tested* it? Does it *work*? – jonrsharpe May 25 '16 at 09:36
  • no, that's implying that it needs some help. – Luke May 25 '16 at 09:43
  • 1
    ...what? Then what's your question? Please read [ask], and cut this down to a [mcve] that demonstrates *what the actual problem is*. – jonrsharpe May 25 '16 at 09:44
  • Could you give me a base for a code – Luke May 25 '16 at 09:47
  • You'll need to at least mention something about _how_ is it not working. – martineau May 25 '16 at 09:47
  • What does that even mean? You seem to *have* *"a base for a code"*; that's the thing that you've dumped into the question without explanation. I'm assuming English isn't your first language - is there someone nearby who could help you explain what exactly you're looking for? – jonrsharpe May 25 '16 at 09:48
  • 1
    Luke: There are literally hundreds if not thousands of examples of using Tkinter on the Internet that have buttons in them. [Here's a few](https://www.google.com/search?as_st=y&tbm=isch&hl=en&as_q=tkinter+example+with+buttons&as_epq=&as_oq=&as_eq=&imgsz=&imgar=&imgc=&imgcolor=&imgtype=&cr=&as_sitesearch=&safe=images&as_filetype=&as_rights=&gws_rd=ssl). – martineau May 25 '16 at 09:49

1 Answers1

2

Answering your posted question "is there anything wrong with this?":

Long story short Yes, there is.

First, your indentation is hopefully not like it looks in your real code but in your post only.

Second, the way you import and use tkinter libraries.

  • import tkinter VS import tkinter as tk:

    please do use either one of them. not both.

  • class Window(Tk):

    this should give an error as you have no access to Tk based on your imports. Either class Window(tkinter.Tk): or class Window(tk.Tk): based on your previous decision.

    the same actions apply to all tkinter references you use inside your code.

Is there another way to solve this? - Yes :

from tkinter import * would give you access to the names directly to make class Window(Tk): work.

Why didn't you tell me that earlier?

One can import all from a library at the beginning. Should one do that? IMHO one should avoid using too many dependencies whenever possible. Why? Because it makes code more lightweight and transferrable. Why should I import things that are not used in my code?

Read the documentation on tkinter carefully. There are , like martineau mentioned , thousands over thousands of example "code bases" around, documentation in all possible formats (books, ebooks, papers, online tutorials ... )

Read the python documentation. At least when you encounter difficulties with certain parts.

Have a look at Q&A before posting new questions. Questions aiming the same goal are available in dozens speaking only for SO.

Some Examples:

All these Questions give examples you can use even if they do not have the same question in mind. These questions provide code examples you can use for your own good.

Community
  • 1
  • 1
R4PH43L
  • 2,122
  • 3
  • 18
  • 30