0

I've been struggling for days trying to figure out the problem in my file. So now I've stripped the whole file down to a minimum with only the problems.

After I added the counting fuction, the problems appeared.

I've been trying several different ways to fix this one without luck. Searched this site up and down and still can't find any answer or questions thats similar to this one with multiple errors.

  • EDIT:
  • The code can now be directly imported. The old import didtnt work as planned.
  • 1st: The problem is that I don't want root to open two windows. But without calling "root=Tk", the "tk.StringVar" will not work.
  • 2nd: The counter only shows the number in console. I want it to show in "l = Label(f3, textvariable=click) # Score"
  • 3rd: What is the root if tk() is allready a "root" without calling root=tk()? And why do I get the error "AttributeError: 'NoneType' object has no attribute '_root'" when I'm not calling root anything?

- I'm not that into Python and Tk yet. So I can't figure out a clever answer myself.

Might be a minder issue for someone with more experience in Python and Tk then me.

Would be extremely glad for any help.

EDIT 2:

UPDATE! Found the problem myself after days of struggling.

Needed to add "self." before "click". Removed "root=tk()", removed "from tkinter import*" and added "tk." for every button, checkbutton, labels and frames and now it finally works. The code is now updated aswell.

import tkinter as tk

Large_font= ("arial", 30)

class Myapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Payment")
        #root.withdraw()
        self.geometry("1280x1024")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (Homepage, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Homepage)
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class Homepage(tk.Frame):
    def __init__(self, parent, controller, **kwargs):
        Frame.__init__(self, parent, **kwargs)
        self.configure(background='grey')
        f1 = tk.Frame(self, width=1200, height=100, bd=3, bg="grey", relief="raise")
        f1.pack(side="top")
        lblInfo = tk.Label(f1, text="MY APP", font=Large_font, bg="grey", fg="white")
        lblInfo.pack(side="top")

#=========SUM UP==========
        f3 = tk.Frame(self, width=400, height=800, bd=3, bg="grey", relief="raise")
        f3.pack(side="right")

        def uiPrint():
            print("")
            print(clickcount)
            blankLine()

        self.click = tk.IntVar()
        self.click.set("6");

        def blankLine():
            for i in range(0):
                print("")

        def buttonCommand():
            global clickcount
            global click
            global mult
            clickcount += 2 * (mult)
            self.click.set(str(clickcount));  # Update score
            uiPrint()

        def buttonCommand1():
            global clickcount
            global click
            global mult
            clickcount -= 1 * (mult)
            self.click.set(str(clickcount));
            uiPrint()

        l = tk.Label(f3, textvariable=click)  # Score
        l.pack()
        plusButton = tk.Button(f3, command = buttonCommand, text="+")
        minusButton = tk.Button(f3, command = buttonCommand1, text="-")
        plusButton.pack(padx=10, pady=10)
        minusButton.pack(padx=10, pady=10)
        btn1 = tk.Button(f3, padx=20, pady=20, bd=8, fg="white", bg="green", font=('arial', 30, 'bold'),
                  text="NEXT")
        btn1.pack(padx=10, pady=10)


clickcount = (6)
mult = 1
dcp1 = 0

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.configure(background='grey')
        f1 = tk.Frame(self, width=600, height=100, bd=3, bg="grey", relief="raise")
        f1.pack()


app = Myapp()
app.mainloop()
Backmann
  • 1
  • 1
  • 1
    What is the problem you are having? [edit] the question and explain what it is you are seeing, including any tracebacks and behaviour you have noticed. Burying details in the title will make no sense to most people. –  Mar 07 '18 at 19:19
  • 2
    Your code will not even run due to several formatting errors. Please read [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and post a testable example. – Mike - SMT Mar 07 '18 at 19:24
  • I think you need to review how classes are set up as all your class methods should have self as the 1st argument. You are attempting to work with variables after the `init` section without creating those variables as attributes. You are importing tkinter twice. This is just messy and can cause problems later. Remove `from tkinter import *` and just leave `import tkinter as tk`. Your spacing is off and makes it hard to read. Please remove unneeded code, for example we don't need do define the app icon or title. Those lines are just in the way. – Mike - SMT Mar 07 '18 at 19:35
  • Wops, looks pretty messy now. Probably a bad import of the file. I'm going to fix this asap. – Backmann Mar 07 '18 at 19:49
  • The root of the problem seems to be the block of code that is just kind of floating there outside of any classes. I'm talking about the five lines of code before the definition of `PageTwo`. Why is that code there rather than in one of the classes? – Bryan Oakley Mar 07 '18 at 20:57
  • Good question. When I insert those lines inside a class, I get the following error: " click.set("6"); NameError: name 'click' is not defined" So how can I define "click"? This counting function is build on a different topic on this site. Link: – Backmann Mar 07 '18 at 21:17
  • https://stackoverflow.com/a/41643647/9458088 – Backmann Mar 07 '18 at 21:22
  • 1
    UPDATE! Found the problem myself after days of struggling. Needed to add "self." before "click". Removed "root=tk()" and now it finally works. Removed "from tkinter import*" as @Mike said and added "tk." for every button, checkbutton, labels and frames. Cheers! – Backmann Mar 07 '18 at 23:24
  • @Backmann Nice work. Always a good feeling to figure out your own coding problem :) – Mike - SMT Mar 08 '18 at 13:51

0 Answers0