How to make KeyCallback fuction work?

import glfw
from OpenGL.GL import *
import numpy as np

currentMode = GL_LINE_LOOP
# or use currentMode as global variable

def render(currentMode):
    #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    
    glBegin(currentMode)
    #Draw Something
    glEnd()

def key_callback(window, key, scancode, action, mods):
    if key==glfw.KEY_1:
        if action==glfw.PRESS:
            currentMode = GL_POINTS
    elif key==glfw.KEY_2:
        if action==glfw.PRESS:
            currentMode = GL_LINES
    elif key==glfw.KEY_3:
        if action==glfw.PRESS:
            currentMode = GL_LINE_STRIP
    
def main():
    if not glfw.init():
        return
    window = glfw.create_window(480, 480, "CG_weekly_practice_03-1_ 2015002606", None, None)
    
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)
    glfw.swap_interval(1)    

    while not glfw.window_should_close(window):
        glfw.poll_events()
        render(currentMode)
        glfw.swap_buffers(window)
        print(currentMode)
    glfw.terminate()
        
if __name__ == "__main__":
    main()

I try to change render fuction parameter, but it doesn’t work in while loop.
What should I do?

This is a Python issue, not a GLFW issue. In Python, you need a global statement to modify global variables from within a procedure:

def key_callback(window, key, scancode, action, mods):
    global currentMode 
    ...

Otherwise, it will treat currentMode as a local variable which is assigned to but never used.

A variable not explicitly declared global is treated as a local variable if it’s assigned to anywhere within the procedure and global if not.

Thanks, but redering didn’t changed. I make print(currentMode) in loop and I checked this parameter changing… But rendering in screen doesn’t change. I don’t know why. I made inital state of currentMode = GL_LINE_LOOP before main(), Should I change the location of code “currentMode = GL_LINE_LOOP”

@MintStout, I’ve deleted your other thread. It was a copy/paste of the code in this one, with no description, and only 2-3 lines changed.

Please read the Forum Posting Guidelines. In particular, Before You Post #5 and Posting Guidelines #3 and #4.

If you have “specific” questions about OpenGL APIs or how to use them, feel free to follow-up with those. But this is not a debugging service. Don’t just post a bunch of code.

Respectfully disagree.
I suspect that (majority of) posters are solo coders , therefore asking for help when they are stuck. I certainly is one.
If that is YOUR “definition of debugging” and if it became more important for posts to fit within forum rules…
Most techies are savvy enough not to spam, no need to worry about forum running out of memory. but frequent " policing" tuns them off , perhaps to another resource.
Cheers

But… it’s not your forum. You can agree with the rules or disagree with them, but the rules don’t belong to you. If you do not wish to abide by them, you don’t have to post here.

However, most of those rules represent what I would call “common courtesy”. If you’re asking to borrow the time of others to help you with a problem, it would be courteous to make sure that you are not wasting the time of the people from whom you’re asking for help. And the most courteous thing one can do when asking for help is to expend ones best efforts to solve the problem on their own before asking for aid. And, failing that, at least ask the question of the right group of people (eg: avoid asking OpenGL experts about Python problems).

We aren’t a resource to be consumed so that you can get back to doing what you want ASAP. We are people. Those rules are just us asking you to treat us like people, not magical answer boxes that solve your problems.

Asking for help when you need help is fine. Asking for help instead of trying to help yourself is less fine. And then there’s the main point: it’s a Python problem in a program that just so happens to involve OpenGL, and this is an OpenGL forum. This simply isn’t the place for the question.

Frequently asking poorly-formed, easily searched for, or off-topic questions turns off the people who have the actual knowledge you seek. The point of those rules is to keep those who have the knowledge engaged by minimizing the number of posts that either don’t match their expertise, are easily solved without them, or otherwise are a waste of their time.

How typical “reply”.
Blame it always on the poster.
So I am taking YOUR advice and not replying to your post.
Any more reply is not worth “social discussion”.
Cheers and have a good day.