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)
python python-3.x tkinter
add a comment |
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)
python python-3.x tkinter
You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance ofTk
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 useself
.
– Mike - SMT
Nov 9 at 14:40
add a comment |
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)
python python-3.x tkinter
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
python python-3.x tkinter
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 ofTk
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 useself
.
– Mike - SMT
Nov 9 at 14:40
add a comment |
You seem to have posted the same code twice. Was that your intention? In a tkinter application you should aways have exactly one instance ofTk
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 useself
.
– 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
add a comment |
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()
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
answered Nov 9 at 14:56
Mike - SMT
8,4932934
8,4932934
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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