Error while making a simple two button game on Python IDLE 3.7











up vote
0
down vote

favorite












I want to make a basic game in which there are two buttons 'left' and 'right' and I have a character. The character should move left and right when the respective buttons are clicked. This is the code I have used. This code is not giving the desired output. Please, can you tell me how to put the two buttons in one area? Also can you please help make this code more efficient?



from tkinter import *

class Application(Frame):

def __init__(a, master):
super(Application, a).__init__(master)
a.grid()
a.b_c = 0
a.b = Button(a)
a.b['text'] = "START"
a.b['command'] = a.u
a.b.grid()


def u(a):
a.b['text'] = "Move Right"
a.b_c += 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Application(root)


class Applicatio(Frame):

def __init__(x, master):
super(Applicatio, x).__init__(master)
x.grid()
x.b_c = 0
x.b = Button(x)
x.b['text'] = "START"
x.b['command'] = x.u
x.b.grid()


def u(x):
x.b['text'] = "Move Left"
a.b_c -= 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Applicatio(root)









share|improve this question
























  • You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
    – Bryan Oakley
    Nov 9 at 14:24










  • Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
    – Mike - SMT
    Nov 9 at 14:40















up vote
0
down vote

favorite












I want to make a basic game in which there are two buttons 'left' and 'right' and I have a character. The character should move left and right when the respective buttons are clicked. This is the code I have used. This code is not giving the desired output. Please, can you tell me how to put the two buttons in one area? Also can you please help make this code more efficient?



from tkinter import *

class Application(Frame):

def __init__(a, master):
super(Application, a).__init__(master)
a.grid()
a.b_c = 0
a.b = Button(a)
a.b['text'] = "START"
a.b['command'] = a.u
a.b.grid()


def u(a):
a.b['text'] = "Move Right"
a.b_c += 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Application(root)


class Applicatio(Frame):

def __init__(x, master):
super(Applicatio, x).__init__(master)
x.grid()
x.b_c = 0
x.b = Button(x)
x.b['text'] = "START"
x.b['command'] = x.u
x.b.grid()


def u(x):
x.b['text'] = "Move Left"
a.b_c -= 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Applicatio(root)









share|improve this question
























  • You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
    – Bryan Oakley
    Nov 9 at 14:24










  • Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
    – Mike - SMT
    Nov 9 at 14:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to make a basic game in which there are two buttons 'left' and 'right' and I have a character. The character should move left and right when the respective buttons are clicked. This is the code I have used. This code is not giving the desired output. Please, can you tell me how to put the two buttons in one area? Also can you please help make this code more efficient?



from tkinter import *

class Application(Frame):

def __init__(a, master):
super(Application, a).__init__(master)
a.grid()
a.b_c = 0
a.b = Button(a)
a.b['text'] = "START"
a.b['command'] = a.u
a.b.grid()


def u(a):
a.b['text'] = "Move Right"
a.b_c += 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Application(root)


class Applicatio(Frame):

def __init__(x, master):
super(Applicatio, x).__init__(master)
x.grid()
x.b_c = 0
x.b = Button(x)
x.b['text'] = "START"
x.b['command'] = x.u
x.b.grid()


def u(x):
x.b['text'] = "Move Left"
a.b_c -= 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Applicatio(root)









share|improve this question















I want to make a basic game in which there are two buttons 'left' and 'right' and I have a character. The character should move left and right when the respective buttons are clicked. This is the code I have used. This code is not giving the desired output. Please, can you tell me how to put the two buttons in one area? Also can you please help make this code more efficient?



from tkinter import *

class Application(Frame):

def __init__(a, master):
super(Application, a).__init__(master)
a.grid()
a.b_c = 0
a.b = Button(a)
a.b['text'] = "START"
a.b['command'] = a.u
a.b.grid()


def u(a):
a.b['text'] = "Move Right"
a.b_c += 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Application(root)


class Applicatio(Frame):

def __init__(x, master):
super(Applicatio, x).__init__(master)
x.grid()
x.b_c = 0
x.b = Button(x)
x.b['text'] = "START"
x.b['command'] = x.u
x.b.grid()


def u(x):
x.b['text'] = "Move Left"
a.b_c -= 1
print("n"*40+" "*a.b_c+"*")

root = Tk()
root.title("Bot World")

app = Applicatio(root)






python python-3.x tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 17:58









Jack Duane

99112




99112










asked Nov 9 at 13:14









Girik Jindal

1




1












  • You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
    – Bryan Oakley
    Nov 9 at 14:24










  • Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
    – Mike - SMT
    Nov 9 at 14:40


















  • You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
    – Bryan Oakley
    Nov 9 at 14:24










  • Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
    – Mike - SMT
    Nov 9 at 14:40
















You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
– Bryan Oakley
Nov 9 at 14:24




You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance of Tk running.
– Bryan Oakley
Nov 9 at 14:24












Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
– Mike - SMT
Nov 9 at 14:40




Your code is hard to follow. Your naming convention makes it hard to read what you are trying to accomplish. You are using an outdated super() method and for some reason have chosen to not use self.
– Mike - SMT
Nov 9 at 14:40












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Your code was a bit hard to follow. Please work on your naming style. PEP8 style guidelines will help here and it will make it easier for others to read your code.



Keep in mind for Python 3 all you need to do with super is super().__init__().



99.9% of the time you only ever need one Tk() instance.



Instead of trying to create 2 separate classes for your buttons you can combine it into one class and it will be easier to manage the b_c class attribute.



import tkinter as tk

class Application(tk.Frame):
def __init__(self, master):
super().__init__()
self.b_c = 0
self.btn1 = tk.Button(self, text="START", command=self.set_buttons)
self.btn1.pack()

def set_buttons(self):
self.btn1.config(text="Move Right", command=self.move_right)
self.btn2 = tk.Button(self, text="Move Left", command=self.move_left)
self.btn2.pack()

def move_right(self):
self.b_c += 1
print("n" * 40 + " " * self.b_c + "*")

def move_left(self):
self.b_c -= 1
print("n" * 40 + " " * self.b_c + "*")


root = tk.Tk()
root.title("Bot World")
Application(root).pack()
root.mainloop()





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226403%2ferror-while-making-a-simple-two-button-game-on-python-idle-3-7%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Your code was a bit hard to follow. Please work on your naming style. PEP8 style guidelines will help here and it will make it easier for others to read your code.



    Keep in mind for Python 3 all you need to do with super is super().__init__().



    99.9% of the time you only ever need one Tk() instance.



    Instead of trying to create 2 separate classes for your buttons you can combine it into one class and it will be easier to manage the b_c class attribute.



    import tkinter as tk

    class Application(tk.Frame):
    def __init__(self, master):
    super().__init__()
    self.b_c = 0
    self.btn1 = tk.Button(self, text="START", command=self.set_buttons)
    self.btn1.pack()

    def set_buttons(self):
    self.btn1.config(text="Move Right", command=self.move_right)
    self.btn2 = tk.Button(self, text="Move Left", command=self.move_left)
    self.btn2.pack()

    def move_right(self):
    self.b_c += 1
    print("n" * 40 + " " * self.b_c + "*")

    def move_left(self):
    self.b_c -= 1
    print("n" * 40 + " " * self.b_c + "*")


    root = tk.Tk()
    root.title("Bot World")
    Application(root).pack()
    root.mainloop()





    share|improve this answer

























      up vote
      1
      down vote













      Your code was a bit hard to follow. Please work on your naming style. PEP8 style guidelines will help here and it will make it easier for others to read your code.



      Keep in mind for Python 3 all you need to do with super is super().__init__().



      99.9% of the time you only ever need one Tk() instance.



      Instead of trying to create 2 separate classes for your buttons you can combine it into one class and it will be easier to manage the b_c class attribute.



      import tkinter as tk

      class Application(tk.Frame):
      def __init__(self, master):
      super().__init__()
      self.b_c = 0
      self.btn1 = tk.Button(self, text="START", command=self.set_buttons)
      self.btn1.pack()

      def set_buttons(self):
      self.btn1.config(text="Move Right", command=self.move_right)
      self.btn2 = tk.Button(self, text="Move Left", command=self.move_left)
      self.btn2.pack()

      def move_right(self):
      self.b_c += 1
      print("n" * 40 + " " * self.b_c + "*")

      def move_left(self):
      self.b_c -= 1
      print("n" * 40 + " " * self.b_c + "*")


      root = tk.Tk()
      root.title("Bot World")
      Application(root).pack()
      root.mainloop()





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Your code was a bit hard to follow. Please work on your naming style. PEP8 style guidelines will help here and it will make it easier for others to read your code.



        Keep in mind for Python 3 all you need to do with super is super().__init__().



        99.9% of the time you only ever need one Tk() instance.



        Instead of trying to create 2 separate classes for your buttons you can combine it into one class and it will be easier to manage the b_c class attribute.



        import tkinter as tk

        class Application(tk.Frame):
        def __init__(self, master):
        super().__init__()
        self.b_c = 0
        self.btn1 = tk.Button(self, text="START", command=self.set_buttons)
        self.btn1.pack()

        def set_buttons(self):
        self.btn1.config(text="Move Right", command=self.move_right)
        self.btn2 = tk.Button(self, text="Move Left", command=self.move_left)
        self.btn2.pack()

        def move_right(self):
        self.b_c += 1
        print("n" * 40 + " " * self.b_c + "*")

        def move_left(self):
        self.b_c -= 1
        print("n" * 40 + " " * self.b_c + "*")


        root = tk.Tk()
        root.title("Bot World")
        Application(root).pack()
        root.mainloop()





        share|improve this answer












        Your code was a bit hard to follow. Please work on your naming style. PEP8 style guidelines will help here and it will make it easier for others to read your code.



        Keep in mind for Python 3 all you need to do with super is super().__init__().



        99.9% of the time you only ever need one Tk() instance.



        Instead of trying to create 2 separate classes for your buttons you can combine it into one class and it will be easier to manage the b_c class attribute.



        import tkinter as tk

        class Application(tk.Frame):
        def __init__(self, master):
        super().__init__()
        self.b_c = 0
        self.btn1 = tk.Button(self, text="START", command=self.set_buttons)
        self.btn1.pack()

        def set_buttons(self):
        self.btn1.config(text="Move Right", command=self.move_right)
        self.btn2 = tk.Button(self, text="Move Left", command=self.move_left)
        self.btn2.pack()

        def move_right(self):
        self.b_c += 1
        print("n" * 40 + " " * self.b_c + "*")

        def move_left(self):
        self.b_c -= 1
        print("n" * 40 + " " * self.b_c + "*")


        root = tk.Tk()
        root.title("Bot World")
        Application(root).pack()
        root.mainloop()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 14:56









        Mike - SMT

        8,4932934




        8,4932934






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226403%2ferror-while-making-a-simple-two-button-game-on-python-idle-3-7%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)