Python Forum
Incrementing variable counter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incrementing variable counter
#1
This is a general question.

Function one uses ypos as variable for the y ie; place(x = 400, y=y_pos)

function two uses it also.


function three uses it also.

I've tried to use it as a Global but doesn't seem to work. What I need to do is use y_pos and increment it.

Don't know how to do this.


Thanks
Reply
#2
There are at least 3 ways to do this:

First, you can use it as a global variable. Show your function code so we can see how you attempted this and point out what could improve it.

Second, you could keep the value as an explicit variable in your main code and pass it to each of the functions as an argument.

Third, you could create a class, turn the functions into methods, and keep the value as an attribute. If you haven't worked with object oriented code before, there might be more steps to this, but in the long run it's more maintainable.
Reply
#3
I have a lot of globals that I may not need, but just learning this language. Any way here we go.

# CREATE WINDOW TO FACILITATE USER CHOOSING FACTOR SETS.
def build_factors():
	global factor_build
	global entry_factors
	global  labelfactor
	global calc_value
	global factor_namee_label
	global factorname
	global factor
	global factor_location
	global calc_valu_pos
	global Factor_ypos
	calc_valu_pos = 140
	Factor_ypos = 140
	calc_value = 0
	factor_build = Toplevel()

	Labe20 = Label(factor_build, text="Factor Factory",fg="#1F51B7",font = ("sans_serif" , 16) ).pack()
	Labe21 = Label(factor_build, text="Choose Wisely Grasshopper",fg="#F31839",font = ("sans_serif" , 12) ).pack()
	labelfactor =Label(factor_build)
	entry_factors = StringVar()
	factornames = StringVar()
	factor = StringVar()
	width_of_window = 1000
	height_of_window = 700
	screen_width= factor_build.winfo_screenwidth()
	screen_height = factor_build.winfo_screenheight()
	x_coordinate = (screen_width/2) - (width_of_window/2)
	y_coordinate = (screen_height/2) - (height_of_window/2)
	factor_build.geometry("%dx%d+%d+%d" % (width_of_window,height_of_window,x_coordinate,y_coordinate ))
	factorset_label= Label( factor_build, text ="Name Your Analyst First", fg= "#F33038", font = ("sans_serif" , 12)).place(x=803, y=20)
	entry_factors = Entry(factor_build, font = ("sans_serif", 12))
	entry_factors.place(x=800, y=50)
	save_factorbutton= Button(factor_build, text="Save Factors Name", width = 15, command=save_factors)
	save_factorbutton.place(x=830, y=80)
	
	#factor_label=Label(factor_build,text = factor, fg="#1F51B7",font = ("sans_serif" , 16) ).place(x=800,y=120)
	factor_build.iconbitmap("c:/guis/racehorse.ico")
	my_button20 = Button(factor_build, text = "DRF Speed",bg="#0DDA7D", width = 15,  command = set_drfvalue) 
	my_button20.place(x=10, y=25)
	my_button21 = Button(factor_build, text = "Bst Spd T-T", bg='#0DDAD7' ,width = 15, command = set_bestspeed) 
	my_button21.place(x=10, y=53)
	my_button22 = Button(factor_build, text = "Bst Spd Turf", bg="#0DDA7D",width = 15, command = set_bestspeed_turf) 


def save_factors():
	global factor
	factor = entry_factors.get()
	factor_namee_label=Label(factor_build,text = factor, fg="#4167E8",font = ("sans_serif" , 14) ).place(x=800,y=145)
	factor_label = Label(factor_build, text = 'Analyst Name', fg= 'black',font = ("sans_serif" , 14) ).place(x=800,y=120)

def set_drfvalue():
	
	get_Caclulator()
	Factor_ypos += 25
	a_factorname= Label( factor_build, text ="drf", fg= "black", font = ("sans_serif" , 16)).place(x=800, y= Factor_ypos) 
I have posted these in reverse order just so you can see what calls what.

I used two different variables for the two displays. Set them at 140 and each time they are used I increment them += 25.
Reply
#4
A function that only reads a variable doesn't need any help to use a global variable. It will check global scope for the value.

A function that assigns a variable must declare it as global or it will be treated as local to the function.

In your save_factors() function, factor_namee_label is assigned, but not declared as global. This means it's local only to the function. The other assigned variables in save_factors() and set_drfvalue() will behave the same way. They won't reference the global. In fact line 55 should generate an error as the local variable won't have a value that can be incremented.
Reply
#5
Thanks so much.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020