-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()
7
  • "is there anything wrong with this?" - why are you asking us?! Have you tested it? Does it work? Commented May 25, 2016 at 9:36
  • no, that's implying that it needs some help. Commented May 25, 2016 at 9:43
  • 1
    ...what? Then what's your question? Please read How to Ask, and cut this down to a minimal reproducible example that demonstrates what the actual problem is. Commented May 25, 2016 at 9:44
  • Could you give me a base for a code Commented May 25, 2016 at 9:47
  • 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. Commented May 25, 2016 at 9:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The feel when you have to upvote an answer but you must downvote the question...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.