Jump to content

Practical problem with Python

Blade_792

i have thise code:

from tkinter import *

window = Tk()
btn= [None]* 9

class Frame:

    #position of button array
    def BTNposition(): 
        i= 0
        c= 0
        k= 0
        while i< 9:
            #declaration of button array
            btn[i] = Button(window, height= 10, width= 20, borderwidth= 3)
            btn[i].place(x= c, y= k)
            i+= 1
            if c == 300:
                k+= 150
                c= 0
            else:
                c+= 150

                
    BTNposition()

    btnp_x= 0
    btnp_y= 0

    def click_position(eventorigin):
        global x,y
        x = eventorigin.x
        y = eventorigin.y
        print(x,y)



    """def GAME():
        btn[i].destroy()
        check what button is pressed and it's coordinates"""

    i= 0
    #while i< 9:
        #btn[i].bind('<Button-1>', GAME)

    window.bind("<Button 1>", click_position)
    
    window.title('PyTris')
    window.geometry("700x450")
    window.mainloop()

i have to take x and y from the function click_position() and use them i other function, since i have about 2 days of experience with python, can someone help me?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Blade_792 said:

i have to take x and y from the function click_position() and use them i other function

There are dozens of ways you could achieve this.

Because I don't know your whole intention (i.e. where the variables have to be used), I will list the general ways to achieve this:

- global variables: by declaring a variable in a place where both functions can find the variables, you can access it from both places

- function parameters: the preferred method, where you call a function and give the values of the variables with it. Would only work if that click_position() function is also the one calling that other function

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

41 minutes ago, minibois said:

- global variables: by declaring a variable in a place where both functions can find the variables, you can access it from both places

thats what i need but i was not shure how to do it thanks.

but when i declare them or in the class or outside the code give me x not defined, and i assume y too.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Blade_792 said:

thats what i need but i was not shure how to do it thanks.

but when i declare them or in the class or outside the code give me x not defined, and i assume y too.

Python is unfortunately not my expertise, so I don't know for sure how it all works in Python, but this is an idea of how it works in other languages (C based languages, like C#):

class ClassName()
{
	int a;

	function FuncName1()
    {
    	// I can access 'int a' just fine here
    	int b;
    }
	
    function FuncName2()
    {
    	// I can access 'int a' just fine here
    	int b;
        // ^not this 'int b' is not the same as 'int b' in 'FuncName1"
    }
}

 

Basically, a global variable is 'int a'. Something you declare in a higher level than the functions.

 

Check out this page as a source: https://www.w3schools.com/python/python_variables_global.asp

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, Blade_792 said:

i have to take x and y from the function click_position() and use them i other function, since i have about 2 days of experience with python, can someone help me?

And what problem are you having with those globals? Best case would to have a class for this.

 

Also tkinter is pretty much obsolete library. The guis look and behave awfully and there is no need on the market for tkinter (at best PyQt but Python is rather rarely used for desktop apps).

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/5/2021 at 11:50 AM, Blade_792 said:

thats what i need but i was not shure how to do it thanks.

but when i declare them or in the class or outside the code give me x not defined, and i assume y too.

Try calling global again in the other function to indicate you're working with a global variable.

 

Also class functions should always have "self" as a first argument. In addition you could store these variables in the class, which you could access through self.x and self.y respectively.

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×